Wordpress php widget - php

I'm using the wordpress php widget but I am unable to process Wordpress shortcode - the php just renders the shortcode instead of processing it. HEre is what I did
Added filters to my active theme function.php file
add_filter('widget_text', 'shortcode_unautop');
add_filter('widget_text', 'do_shortcode');
// Allow shortcodes in php code widget
add_filter('widget_execphp', 'shortcode_unautop');
add_filter('widget_execphp', 'do_shortcode');
Added the following to the php widget
<?php
$id = get_the_ID();
$amazon_product_asin_value = get_post_meta($id, 'amazon_product_asin', true);
echo do_shortcode('<div> [amazon asin=' . $amazon_product_asin_value . '&template=buynowamazon_widget&chan=default] </div>');
?>
I also tried without do_shortcode and same result.
2 properly outputs the shortcode of
[amazon asin=B008I20FT8&template=buynowamazon_widget&chan=default]
which works fine if I just enter this in the standard text widget
I'm using the Amazon Link plugin which generates the shortcodes
Any ideas?

The asin parameter value might be confusing the shortcode processor; try putting quotes around it.
echo do_shortcode('<div> [amazon asin="' . $amazon_product_asin_value . '&template=buynowamazon_widget&chan=default"] </div>');
Alternatively, does the shortcode accept each argument to that parameter as a separate parameter?
echo do_shortcode('<div> [amazon asin="' . $amazon_product_asin_value . '" template="buynowamazon_widget" chan="default"] </div>');
Edit: Looking at the plugin's code, I don't think you need these lines:
add_filter('widget_execphp', 'shortcode_unautop');
add_filter('widget_execphp', 'do_shortcode');
They will try to process the shortcode, before the PHP is executed, and thus you won't get the shortcode correctly processed. Comment them out and see what the plugin does now.

Related

How do I add sharelinkes shortcode in short code

I need to to add a short code into a short code in WordPress.
[mycred_link href="http://www.mycred.me"]View portfolio[/mycred_link]
This short code above is a short code from myCred plugin, it gives users points when they click on the link inside the short code.
What I need is:
To show a Facebook share link, inside the short code using. I already have a short code that generates a Facebook share link.
I need something like this:
[mycred_link href="[facebook_share_link]"]View portfolio[/mycred_link]
To give users points when they share my posts on Facebook. I tried it but it didn't work.
I also tried this code below, it Gives point to users and opens facebook.
But facebook says that URL is invalid.
<?php echo do_shortcode( "[mycred_link href='http://www.facebook.com/sharer/sharer.php?u=<?php echo urlencode(get_permalink()); ?>']View portfolio[/mycred_link]"); ?>
If I put double quote after the href the wordpress footer will disappear completely, am running the second code on my footer.php
The problem is inside the code of myCred plugin. As you can see from the source code of the plugin wp-content/plugins/mycred/includes/shortcodes/mycred_link.php what you pass inside the href attribute doesn't get parsed with do_shortcode therefore you will not be able to insert.
As you can see from this answer:Shortcodes inside a shortcode - wordpress wordpress shortcodes doesn't stack automatically.
Luckily for you the function end with:
return apply_filters( 'mycred_link', '<a ' . implode( ' ', $attr ) . '>' . do_shortcode( $link_title ) . '</a>', $atts, $link_title );
So w/o touching the plugin source code you can use the filter and then do something with the href. My guess is something like this (not tested)
Inside your function.php in your theme folder insert something like this:
add_filter('mycred_link','edit_mycred_link',10,3 );
function edit_mycred_link($link_html, $atts, $link_title){
return str_replace('__edit_mycred_link__',do_shortcode('[facebook_share_link]'),$link_html);
}
And then inside your template file / post content
[mycred_link href="__edit_mycred_link__"]View portfolio[/mycred_link]
That should work
I finally solved the problem. I asked the question on wordpress forum and a moderator gave it to me.
<?php
echo do_shortcode( '[mycred_link href="http://www.facebook.com/sharer/sharer.php?u=' . urlencode( get_permalink()) . '"]View portfolio[/mycred_link]');
?>

wpbakery elements shortcode implement in php

I have created a shortcode through wpbakery Add Element section. I am trying to apply this shortcode in PHP
Here is the shortcode
[vc_masonry_grid post_type="post" max_items="10" grid_id="vc_gid:1592135090064-3fb6b6dd-7144-2" taxonomies="16"]
Here is shortcode with PHP code which I have applied in PHP file but it's showing nothing.
I got this function from here
https://developer.wordpress.org/reference/functions/do_shortcode/
echo do_shortcode( '[vc_masonry_grid post_type="post" max_items="10" grid_id="vc_gid:1591334217992-a1326795-3e97-8" taxonomies="16"]' );

Place shortcode in php template file

this has been discussed before however it's not working for me. As the shortcode seems to be a mix of regular and open-close shortcode. Or a combination of nested shortcodes.
// Use shortcode in a PHP file (outside the post editor).
echo do_shortcode( '' );
// In case there is opening and closing shortcode.
echo do_shortcode( '[iscorrect]' . $text_to_be_wrapped_in_shortcode . '[/iscorrect]' );
This is the shortcode I would like to display in my php template
[mk_page_section bg_stretch="true" full_width="true" padding_top="0" padding_bottom="0" sidebar="sidebar-1"][vc_column][mk_header hover_styles="5" logo="false"][/vc_column][/mk_page_section]
What I am trying to achieve is display my main navigation on a webinar page that doesn't use my themes header or footer.
Is mit possible to use it like this?
<?php echo do_shortcode('[mk_page_section bg_stretch="true" full_width="true" padding_top="0" padding_bottom="0" sidebar="sidebar-1"][vc_column][mk_header hover_styles="5" logo="false"][/vc_column][/mk_page_section]'); ?>
Or must it be a combination? totally lost here...
Why is the do_shortcode not working?

what is "do_shortcode" in wordpress and how it works?

I'm currently learning wordpress theme development and for better understanding I have downloaded couple of wordpress theme and seeing their code.
But a line of code I can't understand. here it is-->
<?php echo do_shortcode(sq_option( 'footer_text ' ' ')); ?>
maybe its for footer but how this works and how i can use this function when I will create my own.
do_shortcode is a function you have to use if you want to execute a shortcode inside a custom theme template page see more info on the function here: https://developer.wordpress.org/reference/functions/do_shortcode/
Generally do_shortcode(); is use to get data from shortcode or its attribute to your PHP code or to filter any shortcode returning data. Shortcode can be provided by any plugin.
Not sure about it but in your theme's code sq_option("footer_text"); could be a function which is filtering some text from footer.
The code could be as:
<?php echo do_shortcode( '[contact-form-7 id="91" title="quote"]' ); ?>
Reference Link
do_shortcode () is usefull function-> for excute shortcode in template file

short code not working in wordpress text editor

I wanted to add shortcode from the text editor of wordpress post.I have added following short code in the post of wordpress:
<img alt="" src="[template_url]/images/ic_sol_1a.png" />
Here [template_url] is the shortcode i wanted to use it was not working. when i see it in the post page it render the text not the short code response. Somewhere i saw a solutions like adding following line to functions.php of theme:
add_filter( 'widget_text', 'shortcode_unautop');
add_filter( 'widget_text', 'do_shortcode');
But still after adding these lines i am unable to make shortcode work. What could be the possible reason?
my shortcode function is like this:
function my_template_url() {
return get_bloginfo('template_url');
}
add_shortcode("template_url", "my_template_url");
You will need to use the get_bloginfo() to return the value as in your example and make sure to use the_content() to print the value to the page. If you use one of the API methods such as get_the_content() then the do_shortcode filter is not run. If you want to apply it use the following:
function my_template_url() {
// This will echo the 'template_url'
return get_bloginfo('template_url');
}
add_shortcode("template_url", "my_template_url");
$shortcoded = apply_filters( 'the_content', get_the_content() );
You do not need the add_filter() lines.
After lots of headache and with the help of #doublesharp and #Nathan Dawson comments/answers we figured out Problem was inside single.php file of theme we were getting the content of post using get_the_content function. By changing it to the_content() function sortcodes start working. Please note that shortcode will not work in the visual editor of WordPress post on admin site but when you see the post actually in the browser by going to that post you can see it working.

Categories