Title: Adding shortcode to Plugin
Last modified: August 7, 2017

---

# Adding shortcode to Plugin

 *  Resolved [jamezon](https://wordpress.org/support/users/jamezon/)
 * (@jamezon)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/adding-shortcode-to-plugin/)
 * Hi,
 * It is easy to add shortcode to a text widget by either using a plugin or adding
   the following to the functions file:
 * add_filter( ‘widget_text’, ‘shortcode_unautop’ );
    add_filter(‘widget_text’, ‘
   do_shortcode’);
 * This enables you to use a shortcode such as a date and then displaying a current
   or dynamic date in your widget sidebar.
 * Is there a way to actually enable shortcode execution (specifically for a dynamic
   or current date) for WordPress Popular Posts?
 * The actual shortcode is showing in the side bar and not the date.
 * WordPress does not support shortcodes in widgets or the sidebar as far as I know,
   unless you add a filter.
 * Can anyone help please?
 * Thanks.

Viewing 15 replies - 1 through 15 (of 18 total)

1 [2](https://wordpress.org/support/topic/adding-shortcode-to-plugin/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/adding-shortcode-to-plugin/page/2/?output_format=md)

 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/adding-shortcode-to-plugin/#post-9387358)
 * Hi there,
 * Sorry, I didn’t understand any of that 😛 Could you please explain what are you
   trying to do?
 *  Thread Starter [jamezon](https://wordpress.org/support/users/jamezon/)
 * (@jamezon)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/adding-shortcode-to-plugin/#post-9387411)
 * Hi Hector,
 * In my post title, in the H1 tag, I add a date automatically by using a shortcode[
   datetoday].
 * So if my if my post title is:
    “How to Understand Shortcode [datetoday]”
 * it shows as:
    “How to Understand Shortcode 2017”
 * When this heading gets pulled into the widget, this is what it shows:
    “How to
   Understand Shortcode [datetoday]”
 * See how it looks currently in the popular posts widget:
    [http://www.spyequipmentguide.com/](http://www.spyequipmentguide.com/)
 * Thanks,
 * James
 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/adding-shortcode-to-plugin/#post-9387609)
 * Ah, I see. Thanks for the explanation!
 * Not sure if there’s a simple way to do what you want since the plugin isn’t expecting
   to find a shortcode in the post title, yours isn’t really a common use case.
 * I’ll give it some thought and get back to you later, alright?
 *  Thread Starter [jamezon](https://wordpress.org/support/users/jamezon/)
 * (@jamezon)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/adding-shortcode-to-plugin/#post-9387695)
 * Thanks.
 * As you can imagine, it is extremely annoying.
 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/adding-shortcode-to-plugin/#post-9388203)
 * You can remove the widget from your sidebar for the time being. As long as the
   plugin is active it’ll keep tracking visits, even if the widget isn’t active.
 *  Thread Starter [jamezon](https://wordpress.org/support/users/jamezon/)
 * (@jamezon)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/adding-shortcode-to-plugin/#post-9388255)
 * Thanks.
 * I’ll leave it up for now.
 * Hopefully you will find an answer.
 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/adding-shortcode-to-plugin/#post-9389401)
 * Alright, try this:
 *     ```
       function bse_custom_single_popular_post( $post_html, $p, $instance ){
   
       	// Post permalink
       	$permalink = get_the_permalink( $p->id );
   
       	// Post title, here's where the magic happens!
       	$post_title = do_shortcode( $p->title ); /* do_shortcode parses shortcodes found in a string */
   
       	// Retrieve post thumbnail
       	$thumbnail = get_the_post_thumbnail( $p->id, array(80, 100), array('class' => 'wpp-thumbnail', 'alt' => esc_attr( $post_title ) ) );
   
       	// OK, now let's put everything together
       	$output = '<li><a href="' . $permalink . '" title="' . esc_attr( $post_title ) . '" target="_self">' . $thumbnail . '</a> <a href="' . $permalink . '" title="' . esc_attr( $post_title ) . '" class="wpp-post-title" target="_self">' . $post_title . '</a></li>';
   
       	return $output;
   
       }
       add_filter( 'wpp_post', 'bse_custom_single_popular_post', 10, 3 );
       ```
   
 * Explanation:
 * The code hooks into [wpp_post](https://github.com/cabrerahector/wordpress-popular-posts/wiki/3.-Filters#wpp_post),
   a filter hook provided by WPP to customize the HTML output of the widget, so 
   we can tweak it to our needs.
 * First, let’s retrieve the URL of your post (we’re literally building the HTML
   ouptut of the widget from scratch here.) Next, we’ll use the [do_shortcode function](https://developer.wordpress.org/reference/functions/do_shortcode/)
   to parse all shortcodes found (if any) in your post titles. Finally, let’s retrieve
   the post thumbnail and then put everything together before returning it to the
   plugin.
 * FYI I didn’t test this but I’m pretty confident it’ll work.
 *  Thread Starter [jamezon](https://wordpress.org/support/users/jamezon/)
 * (@jamezon)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/adding-shortcode-to-plugin/#post-9394197)
 * Thanks Hector,
 * I added the code to the bottom of my functions file (I hope that’s what you actually
   meant I must do)
 * The popular post plugin refused to work at all and some pages gave me a parse
   error on this line:
    $permalink = get_the_permalink( $p->id );
 * Thanks again for your help.
 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/adding-shortcode-to-plugin/#post-9394283)
 * That’s odd, I don’t see anything wrong with that line. I’ll give it a try and
   report back.
 *  Thread Starter [jamezon](https://wordpress.org/support/users/jamezon/)
 * (@jamezon)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/adding-shortcode-to-plugin/#post-9394804)
 * Thanks Hector.
 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/adding-shortcode-to-plugin/#post-9394938)
 * Ah, there was an error indeed. See, most of WordPress functions follow this convention:
    - _the\_function\_name_
    - _**get\_**the\_function\_name_
 * where the former outputs data directly into the screen, and the latter returns
   the data to PHP.
 * The function to retrieve a post/page permalink is (one of) the exception(s): 
   it’s [get_permalink()](https://developer.wordpress.org/reference/functions/get_permalink/),
   not get**_the**_permalink().
 * In short: simply replace `get_the_permalink` with `get_permalink` and it should
   be good now 🙂
    -  This reply was modified 8 years, 9 months ago by [Hector Cabrera](https://wordpress.org/support/users/hcabrera/).
 *  Thread Starter [jamezon](https://wordpress.org/support/users/jamezon/)
 * (@jamezon)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/adding-shortcode-to-plugin/#post-9395152)
 * Hi Hector,
 * I did what you said.
 * Got the following:
 * [Large code excerpt removed by moderator per [forum guidelines](https://wordpress.org/support/guidelines/).
   Please use [Pastebin](https://pastebin.com/) or a [Gist](https://gist.github.com)
   for all large code excerpts, they work better anyway.]
 * Now I am stuck. It still does not work.
 * Thanks again.
    -  This reply was modified 8 years, 9 months ago by [James Huff](https://wordpress.org/support/users/macmanx/).
 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/adding-shortcode-to-plugin/#post-9395426)
 * Ok, just tested it on the Twenty Seventeen theme and it works as expected: [https://prnt.sc/g6f98s](https://prnt.sc/g6f98s)
 * Here’s the revised code:
 *     ```
       function bse_custom_single_popular_post( $post_html, $p, $instance ){
   
       	// Post permalink
       	$permalink = get_permalink( $p->id );
   
       	// Post title, here's where the magic happens!
       	$post_title = do_shortcode( $p->title ); /* do_shortcode parses shortcodes found in a string */
   
       	// Retrieve post thumbnail
       	$thumbnail = get_the_post_thumbnail( $p->id, array(80, 100), array('class' => 'wpp-thumbnail', 'alt' => esc_attr( $post_title ) ) );
   
       	// OK, now let's put everything together
       	$output = '<li><a href="' . $permalink . '" title="' . esc_attr( $post_title ) . '" target="_self">' . $thumbnail . '</a> <a href="' . $permalink . '" title="' . esc_attr( $post_title ) . '" class="wpp-post-title" target="_self">' . $post_title . '</a></li>';
   
       	return $output;
   
       }
       add_filter( 'wpp_post', 'bse_custom_single_popular_post' );
       ```
   
 * Are you sure you’re placing the code in the right spot within your theme’s functions.
   php file?
 * Also, please use the code button when sharing code here. WP forums breaks some
   times when posting raw code like you did.
 *  Thread Starter [jamezon](https://wordpress.org/support/users/jamezon/)
 * (@jamezon)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/adding-shortcode-to-plugin/#post-9395532)
 * Thanks.
 * No more errors but still not working with the Tortuga theme.
 * Where in the functions file should I place the code?
 * I placed the code at the bottom; obviously not the right place.
 * I feel that we are almost there.
 * Thanks,
 * James
 *  Plugin Author [Hector Cabrera](https://wordpress.org/support/users/hcabrera/)
 * (@hcabrera)
 * [8 years, 9 months ago](https://wordpress.org/support/topic/adding-shortcode-to-plugin/#post-9395559)
 * Try placing it at the top of the file. If it still doesn’t work, please upload
   your theme’s functions.php file to pastebin.com so I can take a look.
 * Alternatively, you could just convert that code snippet into a plugin:
 *     ```
       <?php
       /*
       Plugin Name: Custom HTML Markup for WordPress Popular Posts
       Description: Modifies WPP's stock HTML markup with a customized one
       Author: Hector Cabrera
       Author URI: https://cabrerahector.com
       Author Email: me@cabrerahector.com
       License: GPLv2 or later
       License URI: http://www.gnu.org/licences/gpk-2.0.html
       */
   
       function bse_custom_single_popular_post( $post_html, $p, $instance ){
   
       	// Post permalink
       	$permalink = get_permalink( $p->id );
   
       	// Post title, here's where the magic happens!
       	$post_title = do_shortcode( $p->title ); /* do_shortcode parses shortcodes found in a string */
   
       	// Retrieve post thumbnail
       	$thumbnail = get_the_post_thumbnail( $p->id, array(80, 100), array('class' => 'wpp-thumbnail', 'alt' => esc_attr( $post_title ) ) );
   
       	// OK, now let's put everything together
       	$output = '<li><a href="' . $permalink . '" title="' . esc_attr( $post_title ) . '" target="_self">' . $thumbnail . '</a> <a href="' . $permalink . '" title="' . esc_attr( $post_title ) . '" class="wpp-post-title" target="_self">' . $post_title . '</a></li>';
   
       	return $output;
   
       }
       add_filter( 'wpp_post', 'bse_custom_single_popular_post' );
       ```
   
 * Save that as wpp-custom-html-markup.php, place it in your wp-content/plugins/
   folder and activate it through your Dashboard (and remember to delete the code
   from your theme’s functions.php file!)

Viewing 15 replies - 1 through 15 (of 18 total)

1 [2](https://wordpress.org/support/topic/adding-shortcode-to-plugin/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/adding-shortcode-to-plugin/page/2/?output_format=md)

The topic ‘Adding shortcode to Plugin’ 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/)

## Tags

 * [do_shortcode](https://wordpress.org/support/topic-tag/do_shortcode/)
 * [filter](https://wordpress.org/support/topic-tag/filter/)
 * [hook](https://wordpress.org/support/topic-tag/hook/)
 * [wpp_post](https://wordpress.org/support/topic-tag/wpp_post/)

 * 18 replies
 * 2 participants
 * Last reply from: [jamezon](https://wordpress.org/support/users/jamezon/)
 * Last activity: [8 years, 9 months ago](https://wordpress.org/support/topic/adding-shortcode-to-plugin/page/2/#post-9401839)
 * Status: resolved