Title: Custom php function within post_html parameter
Last modified: August 31, 2016

---

# Custom php function within post_html parameter

 *  Resolved [Korveld](https://wordpress.org/support/users/teidar/)
 * (@teidar)
 * [10 years ago](https://wordpress.org/support/topic/custom-php-function-within-post_html-parameter/)
 * Is it possible to add custom php function to the post_html parameter? I need 
   it something like this
 *     ```
       <?php wpp_get_mostpopular(  'wpp_start="<div class=\'posts-wrap\'>"&wpp_end="</div>"&thumbnail_width=1024&thumbnail_height=426&limit=3&excerpt_length=80&excerpt_by_words=1&stats_date_format="F j"&range="all"&post_html="<div class=\'post\'>
         <div class=\'img-block\'>
           <div class=\'date-wrap\'>
             <p class=\'date\'>{date}</p>
           </div>
           {thumb_img}
         </div>'
         . do_action( 'addthis_widget', get_permalink(), get_the_title(), array(
           'type' => 'custom',
           'size' => '32',
           'services' => 'twitter,linkedin,facebook,google_plusone_share',
           'more' => true,
           'counter' => 'bubble_style'
         )); .
         '<div class=\'content\'>
           <h2>{title}</h2>
           <p>{summary}</p>
           <p><a href=\'{url}\'>Lire la suite <img src=\'http://lux.devsua.rocks/wp-content/themes/lux/dist/images/blog/link-arrow.png\' alt=\'Link arrow\' width=\'10\' height=\'26\' /></a></p>
         </div>
       </div>"' ); ?>
       ```
   
 * [https://wordpress.org/plugins/wordpress-popular-posts/](https://wordpress.org/plugins/wordpress-popular-posts/)

Viewing 12 replies - 1 through 12 (of 12 total)

 *  Thread Starter [Korveld](https://wordpress.org/support/users/teidar/)
 * (@teidar)
 * [10 years ago](https://wordpress.org/support/topic/custom-php-function-within-post_html-parameter/#post-7418651)
 * So i want to add share buttons via Share Buttons by AddThis plugin just after
   thumbnail image
 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [10 years ago](https://wordpress.org/support/topic/custom-php-function-within-post_html-parameter/#post-7418738)
 * Hi there!
 * Unfortunately you can’t use PHP code in WordPress’ shortcodes. You’ll have to
   hook into either [wpp_custom_html](https://github.com/cabrerahector/wordpress-popular-posts/wiki/3.-Filters#wpp_custom_html)
   or [wpp_post](https://github.com/cabrerahector/wordpress-popular-posts/wiki/3.-Filters#wpp_post)
   to implement this.
 *  Thread Starter [Korveld](https://wordpress.org/support/users/teidar/)
 * (@teidar)
 * [10 years ago](https://wordpress.org/support/topic/custom-php-function-within-post_html-parameter/#post-7418795)
 * Thank you. I’m not really strong in php, so here what i have achieved so far
 *     ```
       function my_custom_single_popular_post( $post_html, $p, $instance ) {
           $output = '<div class="post">';
           $output .= '<div class="img-block">';
           $output .= '<div class="date-wrap">';
           $output .= '<p class="date">' . date( 'F j', strtotime($p->date) ) . '</p>';
           $output .= '</div>';
           $output .= $thumb_img;
           $output .= '</div>';
           $output .= '<div class="share-buttons">';
           $output .= dynamic_sidebar('share-buttons');
           $output .= '</div>';
           $output .= '<div class="content">';
           $output .= '<h2>' . $p->title . '</h2>';
           $output .= '<p>' . $summary . '</p>';
           $output .= '<p><a href="' . get_the_permalink($p->id) . '">Lire la suite <img src="http://lux.devsua.rocks/wp-content/themes/lux/dist/images/blog/link-arrow.png" alt="Link arrow" width="10" height="26" /></a></p>';
           $output .= '</div>';
           $output .= '</div>';
           return $output;
       }
       add_filter( 'wpp_post', 'my_custom_single_popular_post', 10, 3 );
       ```
   
 * There are two questions. The first one, dynamic sidebar share-buttons now displays
   out of the loop, how can i display it in each post? And the second question, 
   how to output $summary and $thumb_img? I think i’m doing something wrong
 *  Thread Starter [Korveld](https://wordpress.org/support/users/teidar/)
 * (@teidar)
 * [10 years ago](https://wordpress.org/support/topic/custom-php-function-within-post_html-parameter/#post-7418808)
 * Okay I’ve just figured out how to output excerpt and thumbnail image.
 *     ```
       function my_custom_single_popular_post( $post_html, $p, $instance ) {
         $thumb = get_the_post_thumbnail();
         $content = get_the_content();
   
         $output = '<div class="post">';
         $output .= '<div class="img-block">';
         $output .= '<div class="date-wrap">';
         $output .= '<p class="date">' . date( 'F j', strtotime($p->date) ) . '</p>';
         $output .= '</div>';
         $output .= $thumb;
         $output .= '</div>';
         $output .= '<div class="share-buttons">' . dynamic_sidebar('share-buttons') . '</div>';
         $output .= '<div class="content">';
         $output .= '<h2>' . $p->title . '</h2>';
         $output .= '<p>' . wp_trim_words( $content, '80' ) . '</p>';
         $output .= '<p><a href="' . get_the_permalink($p->id) . '">Lire la suite <img src="http://lux.devsua.rocks/wp-content/themes/lux/dist/images/blog/link-arrow.png" alt="Link arrow" width="10" height="26" /></a></p>';
         $output .= '</div>';
         $output .= '</div>';
         return $output;
       }
       add_filter( 'wpp_post', 'my_custom_single_popular_post', 10, 3 );
       ```
   
 * So now i have only one question. How to display widget ‘share-buttons’ within
   the loop?
 *  Thread Starter [Korveld](https://wordpress.org/support/users/teidar/)
 * (@teidar)
 * [10 years ago](https://wordpress.org/support/topic/custom-php-function-within-post_html-parameter/#post-7418813)
 * I’ve tried to display share buttons in different way, but it’s still the same
 *     ```
       function my_custom_single_popular_post( $post_html, $p, $instance ) {
         $thumb = get_the_post_thumbnail();
         $content = get_the_content();
         $share_buttons = do_action( 'addthis_widget', get_permalink(), get_the_title(), array('type' => 'custom','size' => '32','services' => 'twitter,linkedin,facebook,google_plusone_share','more' => true,'counter' => 'bubble_style') );
   
         $output = '<div class="post">';
         $output .= '<div class="img-block">';
         $output .= '<div class="date-wrap">';
         $output .= '<p class="date">' . date( 'F j', strtotime($p->date) ) . '</p>';
         $output .= '</div>';
         $output .= $thumb;
         $output .= '</div>';
         $output .= '<div class="share-buttons">' . $share_buttons . '</div>';
         $output .= '<div class="content">';
         $output .= '<h2>' . $p->title . '</h2>';
         $output .= '<p>' . wp_trim_words( $content, '80' ) . '</p>';
         $output .= '<p><a href="' . get_the_permalink($p->id) . '">Lire la suite <img src="http://lux.devsua.rocks/wp-content/themes/lux/dist/images/blog/link-arrow.png" alt="Link arrow" width="10" height="26" /></a></p>';
         $output .= '</div>';
         $output .= '</div>';
         return $output;
       }
       add_filter( 'wpp_post', 'my_custom_single_popular_post', 10, 3 );
       ```
   
 * Do you have any suggestions?
 *  Thread Starter [Korveld](https://wordpress.org/support/users/teidar/)
 * (@teidar)
 * [10 years ago](https://wordpress.org/support/topic/custom-php-function-within-post_html-parameter/#post-7418815)
 * I just mentioned, it outputs the wrong excerpt, it’s the same on each post. So
   i have question ones again how to output an excerpt?
 *  Thread Starter [Korveld](https://wordpress.org/support/users/teidar/)
 * (@teidar)
 * [10 years ago](https://wordpress.org/support/topic/custom-php-function-within-post_html-parameter/#post-7418824)
 * I’ve tried to use wpp_custom_html. Here’s the code
 *     ```
       // Customize popular posts
       /*
        * Gets the excerpt using the post ID outside the loop.
        */
       function get_excerpt_by_id($post_id){
           $the_post = get_post($post_id); //Gets post ID
           $the_excerpt = $the_post->post_content; //Gets post_content to be used as a basis for the excerpt
           $excerpt_length = 80; //Sets excerpt length by word count
           $the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images
           $words = explode(' ', $the_excerpt, $excerpt_length + 1);
   
           if (count($words) > $excerpt_length) :
               array_pop($words);
               array_push($words, '...');
               $the_excerpt = implode(' ', $words);
           endif;
   
           $the_excerpt = $the_excerpt;
           return $the_excerpt;
       }
       /*
        * Builds custom HTML
        */
       function my_custom_popular_posts_html_list( $mostpopular, $instance ){
           $output = '<div class="posts-wrap">';
   
           // loop the array of popular posts objects
           foreach( $mostpopular as $popular ) {
   
               $excerpt = ''; // Excerpt placeholder
   
               $thumb_img = get_the_post_thumbnail( $popular->id, 'large' );
   
               $share_buttons = do_action( 'addthis_widget', get_permalink(), get_the_title(), array('type' => 'custom','size' => '32','services' => 'twitter,linkedin,facebook,google_plusone_share','more' => true,'counter' => 'bubble_style') );
   
               // Excerpt option checked, build excerpt tag
               if ($instance['post-excerpt']['active']) {
                   $excerpt = get_excerpt_by_id( $popular->id );
                   if ( !empty($excerpt) ) {
                       $excerpt = $excerpt;
                   }
               }
   
               $output .= '<div class="post">';
               $output .= '<div class="img-block">';
               $output .= '<div class="date-wrap">';
               $output .= '<p class="date">' . date( 'F j', strtotime($popular->date) ) . '</p>';
               $output .= '</div>';
               $output .= $thumb_img;
               $output .= '</div>';
               $output .= '<div class="share-buttons">' . $share_buttons . '</div>';
               $output .= '<div class="content">';
               $output .= '<h2>' . esc_attr( $popular->title ) . '</h2>';
               $output .= '<p>' . $excerpt . '</p>';
               $output .= '<p><a href="' . get_the_permalink($popular->id) . '">Lire la suite <img src="http://lux.devsua.rocks/wp-content/themes/lux/dist/images/blog/link-arrow.png" alt="Link arrow" width="10" height="26" /></a></p>';
               $output .= '</div>';
               $output .= '</div>' . "\n";
   
           }
   
           $output .= '</div>';
   
           return $output;
       }
   
       add_filter( 'wpp_custom_html', 'my_custom_popular_posts_html_list', 10, 2 );
       ```
   
 * The excerpt and thumbnails are okay now, but share buttons have the same issue
 *  Thread Starter [Korveld](https://wordpress.org/support/users/teidar/)
 * (@teidar)
 * [10 years ago](https://wordpress.org/support/topic/custom-php-function-within-post_html-parameter/#post-7418830)
 * Finally i’ve able to output share buttons in place i need them. But they shows
   count of the hole page not the single post.
    I’ve added this code to the widget`
   <?php do_action( 'addthis_widget', get_permalink(), get_the_title(), array('type'
   => 'custom','size' => '32','services' => 'twitter,linkedin,facebook,google_plusone_share','
   more' => true,'counter' => 'bubble_style') ); ?>`
 * And here is the latest code in the wpp_custom_html function
 *     ```
       // Customize popular posts
       /*
        * Gets the excerpt using the post ID outside the loop.
        */
       function get_excerpt_by_id($post_id){
           $the_post = get_post($post_id); //Gets post ID
           $the_excerpt = $the_post->post_content; //Gets post_content to be used as a basis for the excerpt
           $excerpt_length = 80; //Sets excerpt length by word count
           $the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images
           $words = explode(' ', $the_excerpt, $excerpt_length + 1);
   
           if (count($words) > $excerpt_length) :
               array_pop($words);
               array_push($words, '...');
               $the_excerpt = implode(' ', $words);
           endif;
   
           $the_excerpt = $the_excerpt;
           return $the_excerpt;
       }
       /*
        * Builds custom HTML
        */
       function my_custom_popular_posts_html_list( $mostpopular, $instance ){
           $output = '<div class="posts-wrap">';
   
           // loop the array of popular posts objects
           foreach( $mostpopular as $popular ) {
   
               $excerpt = ''; // Excerpt placeholder
   
               $thumb_img = get_the_post_thumbnail( $popular->id, 'large' );
   
               ob_start();
               dynamic_sidebar( 'share-buttons' );
               $share_buttons = ob_get_contents();
               ob_end_clean();
   
               // Excerpt option checked, build excerpt tag
               if ($instance['post-excerpt']['active']) {
                   $excerpt = get_excerpt_by_id( $popular->id );
                   if ( !empty($excerpt) ) {
                       $excerpt = $excerpt;
                   }
               }
   
               $output .= '<div class="post">';
               $output .= '<div class="img-block">';
               $output .= '<div class="date-wrap">';
               $output .= '<p class="date">' . date( 'F j', strtotime($popular->date) ) . '</p>';
               $output .= '</div>';
               $output .= $thumb_img;
               $output .= '</div>';
               $output .= '<div class="share-buttons">' . $share_buttons . '</div>';
               $output .= '<div class="content">';
               $output .= '<h2>' . esc_attr( $popular->title ) . '</h2>';
               $output .= '<p>' . $excerpt . '</p>';
               $output .= '<p><a href="' . get_the_permalink($popular->id) . '">Lire la suite <img src="http://lux.devsua.rocks/wp-content/themes/lux/dist/images/blog/link-arrow.png" alt="Link arrow" width="10" height="26" /></a></p>';
               $output .= '</div>';
               $output .= '</div>' . "\n";
   
           }
   
           $output .= '</div>';
   
           return $output;
       }
   
       add_filter( 'wpp_custom_html', 'my_custom_popular_posts_html_list', 10, 2 );
       ```
   
 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [10 years ago](https://wordpress.org/support/topic/custom-php-function-within-post_html-parameter/#post-7418835)
 * Hi there!
 * It seems you’ve been having a nice chat with yourself here hehe. It’s nice to
   see that somehow you managed to do most of the things by yourself, keep up the
   good work!
 * I’m not familiar with the AddThis plugin (and their documentation is a bit lacking
   from a developer point of view). From what I understand from your code you need
   to pass the permalink and the title of the post, and since you’re calling get_permalink()
   and get_the_title() without passing the ID of the post **outside of the loop**
   they won’t work.
 * Try changing this:
 *     ```
       ob_start();
       dynamic_sidebar( 'share-buttons' );
       $share_buttons = ob_get_contents();
       ob_end_clean();
       ```
   
 * … into:
 *     ```
       ob_start();
       do_action( 'addthis_widget', get_permalink( $popular->id ), get_the_title( $popular->id ), array('type' => 'custom', 'size' => '32', 'services' => 'twitter,linkedin,facebook,google_plusone_share', 'more' => true, 'counter' => 'bubble_style') );
       $share_buttons = ob_get_contents();
       ob_end_clean();
       ```
   
 * Let me know how that goes.
 *  Thread Starter [Korveld](https://wordpress.org/support/users/teidar/)
 * (@teidar)
 * [10 years ago](https://wordpress.org/support/topic/custom-php-function-within-post_html-parameter/#post-7418846)
 * Yeah) That’s like most of my developement is going)
    Thanks for advice i’ll try
   it this way and write back
 *  Thread Starter [Korveld](https://wordpress.org/support/users/teidar/)
 * (@teidar)
 * [10 years ago](https://wordpress.org/support/topic/custom-php-function-within-post_html-parameter/#post-7418892)
 * Yes it works like a charm. Thank you so much!
 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [10 years ago](https://wordpress.org/support/topic/custom-php-function-within-post_html-parameter/#post-7418899)
 * Good! Thanks for reporting back 🙂 I’m marking this topic as _resolved_ then.
 * Enjoy the plugin!

Viewing 12 replies - 1 through 12 (of 12 total)

The topic ‘Custom php function within post_html parameter’ is closed to new replies.

 * ![](https://ps.w.org/wordpress-popular-posts/assets/icon-256x256.png?rev=1232659)
 * [WP Popular Posts](https://wordpress.org/plugins/wordpress-popular-posts/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wordpress-popular-posts/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wordpress-popular-posts/)
 * [Active Topics](https://wordpress.org/support/plugin/wordpress-popular-posts/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wordpress-popular-posts/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wordpress-popular-posts/reviews/)

 * 12 replies
 * 2 participants
 * Last reply from: [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * Last activity: [10 years ago](https://wordpress.org/support/topic/custom-php-function-within-post_html-parameter/#post-7418899)
 * Status: resolved