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.
-
Hi there,
Sorry, I didn’t understand any of that 😛 Could you please explain what are you trying to do?
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/Thanks,
James
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?
Thanks.
As you can imagine, it is extremely annoying.
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.
Thanks.
I’ll leave it up for now.
Hopefully you will find an answer.
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, 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 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.
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.
That’s odd, I don’t see anything wrong with that line. I’ll give it a try and report back.
Thanks Hector.
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(), not get_the_permalink().
In short: simply replace
get_the_permalinkwithget_permalinkand it should be good now 🙂-
This reply was modified 8 years, 9 months ago by
Hector Cabrera.
Hi Hector,
I did what you said.
Got the following:
[Large code excerpt removed by moderator per forum guidelines. Please use Pastebin or a Gist 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.
Ok, just tested it on the Twenty Seventeen theme and it works as expected: 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.
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
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!)
The topic ‘Adding shortcode to Plugin’ is closed to new replies.