Wordpress - creating multiple widgets with several fields - widget

I am attempting to create some custom widgets.
I have created one, which only needed a different title field. However I am now stuck when trying to create a widget which allows me to have multiple fields. I need the following fields for the widget:
Title
Introduction text
Main text
Email address
I want the widget form to allow the user to update the fields above with text. I then want to output the text they have written in my sidebar widget.
I have the following code which is for the previous widget I made (with no extra fields other than title)
class registercv_widget extends WP_Widget {
function __construct() {
parent::__construct(
'registercv_widget',
__('Register CV Widget', 'registercv_widget_domain'),
array( 'description' => __( 'Provides a "Register CV" button which launches a pop-up', 'registercv_widget_domain' ), )
);
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
echo $args['before_widget'];
echo '<div class="widget-wrapper">';
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
// This is where you run the code and display the output
echo __( '<div class="register-button"><span><div class="button white">Send your CV</div></span></div>', 'registercv_widget_domain' );
echo '</div>';
echo $args['after_widget'];
}
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'Register as a candidate', 'registercv_widget_domain' );
}
// Widget admin form
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}
function registercv_load_widget() {
register_widget( 'registercv_widget' );
}
add_action( 'widgets_init', 'registercv_load_widget' );
This works fine but how can I add extra fields to this?
I have attempted copying all instances of $title and making them again but with $text_one but I do not understand how this field is being registered. Can anybody help?

Found the solution.
I started with the code above and was able to create multiple fields as such:
echo $args['before_widget'];
echo '<div class="widget-wrapper">';
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
if ( ! empty( $text_two ) )
echo '<p>' . $text_two . '</p>';
if ( ! empty( $link_text ) )
echo '<p>' . $link_text . '<i class="fa fa-play"></i></p>';
echo '</div>';
echo $args['after_widget'];
The above example makes use of $text_two and $link_text variables which can be set through the widget screen with use of the following:
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'Executive Search', 'executive_widget_domain' );
}
if ( isset( $instance[ 'text_two' ] ) ) {
$text_two = $instance[ 'text_two' ];
}
else {
$text_two = __( 'Systematic searching to map the right talent for Director level roles using the best possible sector intelligence.', 'executive_widget_domain' );
}
if ( isset( $instance[ 'link_text' ] ) ) {
$link_text = $instance[ 'link_text' ];
}
else {
$link_text = __( 'Visit Executive Search', 'executive_widget_domain' );
}
// Widget admin form
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
<br /><br />
<label for="<?php echo $this->get_field_id( 'text_two' ); ?>"><?php _e( 'Main text:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'text_two' ); ?>" name="<?php echo $this->get_field_name( 'text_two' ); ?>" type="text" value="<?php echo esc_attr( $text_two ); ?>" />
<br /><br />
<label for="<?php echo $this->get_field_id( 'link_text' ); ?>"><?php _e( 'Link text' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'link_text' ); ?>" name="<?php echo $this->get_field_name( 'link_text' ); ?>" type="text" value="<?php echo esc_attr( $link_text ); ?>" />
</p>
<?php
}
Entire code snippet here which can be popped right into your project. http://pastebin.com/X7wXmcds
Placing the above code in your functions.php will give you a widget called Executive Search. It has three fields:title,text_two(a simple text string) andlink text`. So those are the three fields the user can add.

Related

Relationship Field meta_query Not Working Why?

<?php
/*
* Query posts for a relationship value.
* This method uses the meta_query LIKE to match the string "123" to the database value a:1:{i:0;s:3:"123";} (serialized array)
*/
$doctors = get_posts(array(
'post_type' => 'star',
'meta_query' => array(
array(
'key' => 'location', // name of custom field
'value' => '"' . get_the_ID() . '"', // can you please explain this? my relationship field is currently Post Object not sure how
'compare' => 'LIKE'
)
)
));
?>
<?php if( $doctors ): ?>
<ul>
<?php foreach( $doctors as $doctor ): ?>
<?php
$photo = get_field('photo', $doctor->ID);
?>
<li>
<a href="<?php echo get_permalink( $doctor->ID ); ?>">
<img src="<?php echo $photo['url']; ?>" alt="<?php echo $photo['alt']; ?>" width="30" />
<?php echo get_the_title( $doctor->ID ); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
base on the documentation of acf this is the right code but testing it not working why?..........................................thank you guys for helping me..

border-bottom length in tables td's

I searched everywhere for a solution and can't mange to work it out.
I want to control the border bottoms length for td elements in a table.
I put two screenshots.
Any help? thank you!
This is the current result :
This is what it should look like:
edit:
This is the code (it's woocommerce order page)
<tr class="order">
<td class="order-number" data-title="<?php _e( 'Order Number', 'woocommerce' ); ?>">
<a href="<?php echo esc_url( $order->get_view_order_url() ); ?>">
#<?php echo $order->get_order_number(); ?>
</a>
</td>
<td class="order-date" data-title="<?php _e( 'Date', 'woocommerce' ); ?>">
<time datetime="<?php echo date( 'Y-m-d', strtotime( $order->order_date ) ); ?>" title="<?php echo esc_attr( strtotime( $order->order_date ) ); ?>"><?php echo date_i18n( get_option( 'date_format' ), strtotime( $order->order_date ) ); ?></time>
</td>
<td class="order-status" data-title="<?php _e( 'Status', 'woocommerce' ); ?>" style="text-align:left; white-space:nowrap;">
<span><?php echo wc_get_order_status_name( $order->get_status() ); ?></span>
</td>
<td class="order-total" data-title="<?php _e( 'Total', 'woocommerce' ); ?>">
<span><?php echo sprintf( _n( '%s for %s item', '%s for %s items', $item_count, 'woocommerce' ), $order->get_formatted_order_total(), $item_count ); ?></span>
</td>
<td class="order-actions">
<?php
$actions = array();
if ( in_array( $order->get_status(), apply_filters( 'woocommerce_valid_order_statuses_for_payment', array( 'pending', 'failed' ), $order ) ) ) {
$actions['pay'] = array(
'url' => $order->get_checkout_payment_url(),
'name' => __( 'Pay', 'woocommerce' )
);
}
if ( in_array( $order->get_status(), apply_filters( 'woocommerce_valid_order_statuses_for_cancel', array( 'pending', 'failed' ), $order ) ) ) {
$actions['cancel'] = array(
'url' => $order->get_cancel_order_url( wc_get_page_permalink( 'myaccount' ) ),
'name' => __( 'Cancel', 'woocommerce' )
);
}
$actions['view'] = array(
'url' => $order->get_view_order_url(),
'name' => __( 'View', 'woocommerce' )
);
$actions = apply_filters( 'woocommerce_my_account_my_orders_actions', $actions, $order );
if ( $actions ) {
foreach ( $actions as $key => $action ) {
echo '' . esc_html( $action['name'] ) . '';
}
}
?>
</td>
</tr>
Add border botted for "td" as dotted .
border-bottom:1px dotted #000;
Please add the below code.
border-collapse: separate;
border-bottom:1px dotted color;
You can't control the border length of any element...
Instead, you can control the width of the element itself and then apply the property border-bottom.
Now if you need that the element doesn't have full available width... you can make use of margin alongside width display (because we are talking about tables)
Ex.
td {
margin: 10px; <-- to separete the cell from the border
display: inline-block; <-- replace the default "table-cell"
border-bottom: 1px dotted #999; <-- the dotted border
}
See example: http://jsfiddle.net/gmolop/jn846ypv/

Add category slugs to loop in wordpress

I don't know if I've used the best title for this question but I'm not sure exactly how to phrase it. I've successfully setup a filterable portfolio script on my site but I need to apply the appropriate class to each item. Right now I've got this.
<?php $loop = new WP_Query( array( 'post_type' => 'productions', 'posts_per_page' => -1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="tile web all"> <?php the_post_thumbnail ( 'home-page' ); ?>
<h1><?php the_title(); ?></h1>
</div>
<?php endwhile; wp_reset_query(); ?>
The class "web" is just an example, it needs to be replaced by the slug(s) for the categories used for that particular post, as I've setup the filters to automatically show all the categories like this:
<div class="filters">
<p href="" data-rel="all">All</p>
<?php
$args = array(
'type' => 'productions',
'orderby' => 'name',
'order' => 'ASC',
'taxonomy' => 'production_type',
);
$categories = get_categories($args);
foreach($categories as $category) {
echo '<p data-rel="' . $category->slug.'">' . $category->name.'</p> ';
}
?>
Hopefully that is enough info for some help. Thanks.
This worked:
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="tile <?php
$terms = get_the_terms( $post->ID , 'production_type' );
foreach ( $terms as $term ) {
echo $term->slug;
echo ' ';
}
?> all"> <?php the_post_thumbnail ( 'home-page' ); ?>
<h1><?php the_title(); ?></h1>
</div>
<?php endwhile; wp_reset_query(); ?>

Can't figure out why this drop down categories form is not working. Not sure how to correctly add arguments

I am creating a Wordpress recent posts widget with a bunch of different functions for practice. One of the functions I need it to do was allow the admin to specify one category to display via a drop down menu with the categories on it. I asked for help with this here and got an answer but I am not understanding how to get it to work properly. Here is my code:
<?php
/*
Plugin Name: News Recent Posts Widget
Plugin URI:
Description: A recent post widget with extra functions that allow admin to make changes to certain values
Author: Kevin Ullyott
Version: 1.0
Author URI: http://modmacro.com/
*/
class recentpost extends WP_Widget {
public function __construct() {
parent::WP_Widget(
// or parent::__construct(
false,
'Kevin - Recent Posts Widget',
array(
'description' => __('A recent post widget with extra functions that allow admin to make changes to certain values')
)
);
;
}
public function widget( $args, $instance ) {
extract( $args );
$headline = $instance['headline'];
$category = $instance['category'];
$numberposts = $instance['numberposts'];
$readmore = $instance['readmore'];
echo $before_widget;
echo $before_title;
echo "<p class=\"headline\">$headline</p>";
echo $after_title;
$args = array( 'numberposts' => $numberposts, 'category_name' => $category );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
setup_postdata(get_post($recent['ID']));
echo '<a href="' . get_permalink($recent['ID']) . '" title=" '.esc_attr(get_the_title($recent['ID'])).'" >' . get_the_title($recent['ID']).'</a> ';
echo get_the_time('F j, Y', $recent['ID']);
the_excerpt();
}
wp_reset_postdata();
echo $after_widget;
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['headline'] = ( $new_instance['headline'] );
$instance['category'] = ( $new_instance['category'] );
$instance['numberposts'] = ( $new_instance['numberposts'] );
$instance['readmore'] = ( $new_instance['readmore'] );
return $instance;
}
public function form( $instance ) {
$headline = $instance[ 'headline' ];
$category = $instance[ 'category' ];
$numberposts = $instance[ 'numberposts' ];
$readmore = $instance[ 'readmore' ];
?>
<p>
<label for="<?php echo $this->get_field_id( 'headline' ); ?>">
<?php _e( 'Headline:' ); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'headline' ); ?>" name="<?php echo $this->get_field_name( 'headline' ); ?>" type="text" value="<?php echo esc_attr( $headline ); ?>" />
</p>
<label for="<?php echo $this->get_field_id( 'category' ); ?>">
<?php _e( 'Category:' ); ?>
</label>
<?php wp_dropdown_categories(array('name' => $this->get_field_name('category'), 'selected' => $category, 'id' => $this->get_field_id('category'), 'class' => 'widefat')); ?>
</p>
<label for="<?php echo $this->get_field_id( 'numberposts' ); ?>">
<?php _e( 'Number of posts:' ); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'numberposts' ); ?>" name="<?php echo $this->get_field_name( 'numberposts' ); ?>" type="text" value="<?php echo esc_attr( $numberposts ); ?>" />
</p>
<label for="<?php echo $this->get_field_id( 'readmore' ); ?>">
<?php _e( 'Read More:' ); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'readmore' ); ?>" name="<?php echo $this->get_field_name( 'readmore' ); ?>" type="text" value="<?php echo esc_attr( $readmore ); ?>" />
</p>
<?php
}
}
add_action( 'widgets_init', create_function('', 'return register_widget("recentpost");') );
?>
So if you look down at the form for Category I put in a PHP code there to create the drop down menu. It succesfully creates the form and displays the correct categories within the drop down menu but when I save it does not correctly update and makes the widget display nothing.
The user that gave me this code told me this. He said: "function wp_dropdown_categories will bring in the categories but adding the function alone wont save or retrieve the selected category. You'll need to add the name selected and id arguments." But the problem is I do not know how to add the name, selected, and id arguments. Everything I have tried to do has fail so far. Can some one show me the correct way to do this using my code. It would help a lot. I am very new to this and am trying to learn so please provide what I need to do and if you could explain it.
Sorry to ask so much I just enjoy coding and want to know as much as I can!
wp_dropdown_categories returns the category ID. so, if your form displaying the correct categories and saving, then its working. So youll want to look at your query. It looks like you are currently trying to get categories by category_name.
$args = array( 'numberposts' => $numberposts, 'category_name' => $category );
change that to
`$args = array( 'numberposts' => $numberposts, 'cat' => $category );

Recent posts wordpress widget, with limited text and 'read more' link

I am creating a Wordpress Recent Posts widget for practice. However I have run into a few things that I just don't know how to do. First of all the widget has to display the date and time of the post, the posts title, text with a smart cut off character limit and a read more link. It also has to allow the admin to specify the title, the category, the number of posts to display, number of characters to display on the post, and lastly change the wording of the 'read more' link.
I have already created the widget and figured out how to do everything but make the category selection a drop down selection with the categories already in there (right now it is just a text block for the admin that you can type in which category to display), how to limit characters in the_excerpt while making it so it smartly cuts off at the end of a word and allowing admin to specify how my characters are allowed, and how to create a read more link while allowing admin to specify what it says.
I will post the code I have so far below along with a link to the site that contains the widget in the sidebar. I am new to Wordpress Widget design so help would be greatly appreciated.
<?php
/*
Plugin Name: News Recent Posts Widget
Plugin URI:
Description: A recent post widget with extra functions for client management
Author: Kevin Ullyott
Version: 1.0
Author URI: http://modmacro.com/
*/
class recentpost extends WP_Widget {
public function __construct() {
parent::WP_Widget(
// or parent::__construct(
false,
'Kevin - Recent Posts Widget',
array(
'description' => __('A recent post widget with extra functions for client management')
)
);
;
}
public function widget( $args, $instance ) {
extract( $args );
$headline = $instance['headline'];
$category = $instance['category'];
$numberposts = $instance['numberposts'];
$readmore = $instance['readmore'];
echo $before_widget;
echo $before_title;
echo "<p class=\"headline\">$headline</p>";
echo $after_title;
$args = array( 'numberposts' => $numberposts, 'category_name' => $category );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
setup_postdata(get_post($recent['ID']));
echo '<a href="' . get_permalink() . '" title=" '.esc_attr(get_the_title()).'" >' . get_the_title().'</a> ';
echo get_the_time('F j, Y', $recent['ID']);
the_excerpt();
}
wp_reset_postdata();
echo $after_widget;
}
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['headline'] = ( $new_instance['headline'] );
$instance['category'] = ( $new_instance['category'] );
$instance['numberposts'] = ( $new_instance['numberposts'] );
$instance['readmore'] = ( $new_instance['readmore'] );
return $instance;
}
public function form( $instance ) {
$headline = $instance[ 'headline' ];
$category = $instance[ 'category' ];
$numberposts = $instance[ 'numberposts' ];
$readmore = $instance[ 'readmore' ];
?>
<p>
<label for="<?php echo $this->get_field_id( 'headline' ); ?>">
<?php _e( 'Headline:' ); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'headline' ); ?>" name="<?php echo $this->get_field_name( 'headline' ); ?>" type="text" value="<?php echo esc_attr( $headline ); ?>" />
</p>
<label for="<?php echo $this->get_field_id( 'category' ); ?>">
<?php _e( 'Category:' ); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'category' ); ?>" name="<?php echo $this->get_field_name( 'category' ); ?>" type="text" value="<?php echo esc_attr( $category ); ?>" />
</p>
<label for="<?php echo $this->get_field_id( 'numberposts' ); ?>">
<?php _e( 'Number of posts:' ); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'numberposts' ); ?>" name="<?php echo $this->get_field_name( 'numberposts' ); ?>" type="text" value="<?php echo esc_attr( $numberposts ); ?>" />
</p>
<label for="<?php echo $this->get_field_id( 'readmore' ); ?>">
<?php _e( 'Read More:' ); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id( 'readmore' ); ?>" name="<?php echo $this->get_field_name( 'readmore' ); ?>" type="text" value="<?php echo esc_attr( $readmore ); ?>" />
</p>
<?php
}
}
add_action( 'widgets_init', create_function('', 'return register_widget("recentpost");') );
?>
http://www.modmacro.us/wpsandbox/
A lot to this question. I think it's great you're tackling this as a practice exercise. That's exactly how I learned. I can help with a few parts.
categories
In the form function, first you need to get a list of all the categories using WordPress' get_categories function.
$categories = get_categories(array('type'=>'post','orderby'=> 'name','order'=> 'ASC'));
Then, to display a drop-down menu in the form, you use a for-loop to go through each category.
echo '<select name="' . $this->get_field_name('category') . '" id="' . $this->get_field_id('category') . '">';
foreach($categories as $category):
echo ' <option value="' . $category->slug .'" '. selected($category->slug, $instance['category'], false) . '>' . $category->name . '</option>';
endforeach;
echo '</select>';
Show posts date/time, etc
I think it's better to use the WP_Query class, which is more flexible and seems to be the preferred route these days. In your widget function, it would look something like this
$args = array( 'numberposts' => $numberposts, 'category_name' => $category );
$myquery = new WP_Query($args);
while($myquery->have_posts()): $myquery->the_post();
echo '<a href="' . get_permalink() . '" title=" '.esc_attr(get_the_title()).'" >' . get_the_title().'</a> ';
echo get_the_time('F j, Y', $myquery->ID);
the_excerpt();
endwhile;
wp_reset_query();
wp_reset_postdata();
Limit Excerpt
This will apply to all uses of excerpts in your theme however.
function new_excerpt_length($length) {
return 20;
}
add_filter('excerpt_length', 'new_excerpt_length');