How to run php code only for a specific widget on a page and not all widgets on that page?

Seeing as it’s in a loop, you should be able to check against the type of post, and only add it to that type. function add_rating_html( $title) { if ( is_front_page() && ‘post’ === get_post_type() ) { $out = “Rating: 5”; $title .= $out; } return $title; } add_filter( ‘the_title’, ‘add_rating_html’, 1 ); There’s probably … Read more

How to add a recent post function to a text widget I created for practice?

In the function widget( $args, $instance ), which generates the widget output, add the following code before the echo $after_widget; line: // The Query $the_query = new WP_Query( array( ‘post_type’ => ‘page’, ‘posts_per_page’ => ‘3’ ) ); // The Loop while ( $the_query->have_posts() ) : $the_query->the_post(); echo ‘<li>’ . get_the_title() . ‘</li>’; endwhile; Additional information … Read more

accessing wordpress serialized data outside wp

Use PHP’s unserialize function: <?php $data=”a:2:{i:0;s:12:”Sample array”;i:1;a:2:{i:0;s:5:”Apple”;i:1;s:6:”Orange”;}}”; $unserialized = unserialize($data); echo ‘<pre>’; print_r($unserialized); echo ‘</pre>’; ?> Result: Array ( [0] => Sample array [1] => Array ( [0] => Apple [1] => Orange ) ) Got the sample data over at unserialize.com, a handy little site if you want to quickly check what’s inside that … Read more

Input with pattern not working

Most likely because widgets are saved via AJAX, and, as far as I’m aware, the submit handler will bypass any HTML5 input rules unless they are explicitly checked with JavaScript. Your best bet is to validate/sanitize the data server-side, and pass back any messages if there is an input error. Chrome adding =”” is just … Read more

WordPress Widget Not Saving Instance

The problem is your update function. $old_instance is kind of special variable. The simplest is function update($new_instance, $old_instance) { return $new_instance; } For the safe function update($new_instance, $old_instance) { $instance = array(); $instance[‘name’] = htmlentities($new_instance[‘name’]); $instance[‘divclass’] = htmlentities($new_instance[‘divclass’]); $instance[‘paypal’] = htmlentities($new_instance[‘paypal’]); return $instance; }