I am making a custom theme with shortcodes. The theme is ready, but now I need to make the shortcodes.
The problem is that the shortcodes don't work in my posts, they are only visible on my pages. When I add a shortcode in my post, the post displays nothing.
Here is the shortcode code:
function rss_subscribe_link() {
return 'Enjoy what you\'re reading? Subscribe to our RSS feed here now.'; }
add_shortcode('rss-subscribe', 'rss_subscribe_link');
add_filter('widget_text', 'do_shortcode');
Does anyone know how to solve this problem?
Greets,
Joren
This could happen if your post content is displayed via get_the_content() which is a function that returns the content without applying the default filters (wpautop, do_shortcode etc) that are applied normally when you use the_content(). Could you confirm if this is the case?
Related
WordPress Divi Theme shortcode adding problem
My Divi version is 3.20.1. I am trying to add my own custom shortcode in the website. However, when I add this shortcode, the elements displayed using this shortcode appear both in the "Edit page" area top section besides the main page.
add_shortcode( "Btx_Show_Testimonial_Main_Page", 'lantry_btx_fun_Main_Page_Show_Testimonial');
function lantry_btx_fun_Main_Page_Show_Testimonial(){
include_once LANTRY_BITECHX_SHORTCODE_DIR_PATH."views/Main_Page_testimonial_show.php";
}
My question is, how can I remove this from the "Edit page" section ??
I provided some screenshot.
Divi module Image Option Select
Show top of the post page
When I remove Divi this problem will be solved. But I need to use Divi.
You can check if a adminuser is logged in and if not execute your function hope this helps but now you have to always log out or use incognito mode to see if its there:
add_shortcode( "Btx_Show_Testimonial_Main_Page", 'lantry_btx_fun_Main_Page_Show_Testimonial');
function lantry_btx_fun_Main_Page_Show_Testimonial(){
if (current_user_can( 'update_core' )) {
return;
}
include_once LANTRY_BITECHX_SHORTCODE_DIR_PATH."views/Main_Page_testimonial_show.php";
}
I don't think the problem comes from DIVI but from your shortcode. Please attach the content of your file Main_Page_testimonial_show.php
The shortcode callback shouldn't produce an output but should return the result.
You can find this on documentation website here. Please pay attention to
Note that the function called by the shortcode should never produce an
output of any kind. Shortcode functions should return the text that is
to be used to replace the shortcode. Producing the output directly
will lead to unexpected results. This is similar to the way filter
functions should behave, in that they should not produce expected side
effects from the call since you cannot control when and where they are
called from.
Here, simply you can create the shortcode in the functions.php file and on your Page/Posts you can use the Text Module and add the [shortcode].
I'm using shortcodes in my Wordpress page to display a responsive Google Map centered at specific location. To do it I'm using shortcode supported by external Wordpress plugin. Such shortcode is let's say [map address="New York"].
As all my Wordpress posts titles are names of the cities, I would like to automatize the process and be able to display the titles of the post in place of the address parameter in [map] shortcode.
Is it possible? Is there any way to do it? As far as I know nesting code or variables in shortcode isn't supported by Wordpress but maybe there is some kind of a workaround.
The same behavior I'd like to accomplish for 'custom field' - really similar situation.
I'd appreciate any help and any advice
Try to put this code to functions.php file in your theme:
function mymap_shortcode() {
echo do_shortcode('[map address="'.esc_html( get_the_title() ).'"]');
}
function mymap_shortcodes_init() {
add_shortcode('mymap', 'mymap_shortcode');
}
add_action('init', 'mymap_shortcodes_init');
And then in posts/pages (or even put it directly to template, or create another filter to add that) use shortcode [mymap] that will run [map] shortcode with current page title as parameter.
I'm using the default RSS feed to pass some content through to some subsites I manage. But there are some shortcodes which are being rendered, but should be rendered by the subsites.
So how do I prevent the shortcodes from being parsed?
The shortcode it's about is from a plugin.
[wb-rekentool "username/othername"]
I solved the problem! Used the "the_content_feed" filter to remove the shortcode:
function remove_sc_feed($content){
remove_shortcode('wb-rekentool');
return $content;
}
add_filter('the_content_feed', 'remove_sc_feed');
Easy, but the answer was nowhere to be found!
I am currently developing a theme for Wordpress 3.8.1. As my theme will not display any tags, so I want do disable them (only from the posts, not from custom post types). But how do I do this? I have tried this, but apparently, it does nothing:
register_taxonomy('post_tag', null);
To be clear: I do not just want to hide the tags in the template files, but I want to disable them completely, so in the backend, there is no menu item for tags under posts.
Is it even possible? I hope so. Thanks for your help!
Update
Additionally, I have tried the following, without any effect:
register_taxonomy('post_tag', array());
and
global $wp_taxonomies;
$taxonomy = 'post_tag';
if(taxonomy_exists($taxonomy))
unset($wp_taxonomies[$taxonomy]);
Both remove the tags box while editing a post, but there still is the link in the menu pointing to the list of tags!
As of WordPress 3.7, there is an unregister_taxonomy_for_object_type function available for just this sort of thing.
In your case:
// Remove tags support from posts
function myprefix_unregister_tags() {
unregister_taxonomy_for_object_type('post_tag', 'post');
}
add_action('init', 'myprefix_unregister_tags');
View the documentation for this function here.
Paste this code into your functions.php
add_action( 'admin_menu', 'myprefix_remove_meta_box');
function myprefix_remove_meta_box(){
remove_meta_box( 'tagsdiv-post_tag','post','normal' );
}
tags meta box has a class of tagsdiv-post_tag, so this will remove the tags meta box
OR
add_action('init', 'remove_tags');
function remove_tags(){
register_taxonomy('post_tag', array());
}
if you completely want to remove it
The best solution to disable rewrite rules for tags in posts for me was to use the post_tag_rewrite_rules filter hook.
add_filter( 'post_tag_rewrite_rules', '__return_empty_array' );
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.