Adding Wordpress function into shortcode - php

I have a shortcode [table id=table /]
I want to do something like this <?php echo do_shortcode('[table id="'.$post->post_name.'"/]'); ?>
I want the slug to appear as the table id.
What am I doing wrong?

Given that you mentioned in the comments that you're trying to use this shortcode in a widget - widgets don't parse shortcodes by default.
To make most widgets parse shortcodes (most being those with text fields that apply the widget_text filter), you can add the following to your theme's functions.php:
add_filter("widget_text", "do_shortcode");
You can then refer to this shortcode directly in your widget text like you would expect you can:
[table id=... /]
EDIT:
If you have any trouble running shortcodes that are on their own line, it may be because they get wrapped in <p></p> tags automatically by Wordpress. To stop this happening, add this filter before the one added above:
add_filter("widget_text", "shortcode_unautop");
The shortcode_unautop() function simply stops shortcodes from being wrapped in paragraph tags like is Wordpress' default behaviour in most text fields.

Related

WordPress forces a line break after each shortcode. How to fix?

I just can't figure this out. Wordpress seems to be adding a br tag at the end of each shortcode. What I'm trying to do is the following:
This deal is posted on [shortcode] and is available via [shortcode]
What Wordpress actually does:
This deal is posted on [shortcode]
and is available via [shortcode]
It's a shortcode I made with a custom field. I fill in a certain form and the shortcode spits whatever I filled in in that form.
If you check this link, the text which displays "Aanbieding geldig bij: Amazon.nl" 3 times, should display on one line. "Amazon.nl" is displayed by the shortcode.
What do I do to have it not put a line break after a shortcode?
It's in your style rh-flex-center-align
change it to
.rh-flex-center-align{
display:inline-flex;
}
Or, if you can't do that, inject the style inline on the element generated through the shortcode
<div class="rh-flex-center-align" style="display: inline-flex;">
<span class="meta_v_label mr5 rtlml5">Aanbieding geldig bij: </span> Amazon.nl
</div>

Displaying raw shortcodes in a Wordpress page

I am using a Wordpress theme Flatsome which uses brackets [] in visual editor to output content. I would like to insert a function to theme functions.php which would disable content output and only display the shortcodes as raw text + wrap it in pre tags, just like this:
final result
Currently I am using this function to achieve the result, but it messes up the flow of other content:
function fv_render_raw_content($atts, $content = null)
{
echo '<pre>' . htmlspecialchars($content) . '</pre>';
}
add_shortcode('render_raw_content', 'fv_render_raw_content');
It causes headings inside the page (not wrapped by the function) to change order for no reason:
the problem
I would really appreciate any ideas on what to change so other content flows according to the Wordpress visual editor and is not affected by the function. Thank you!
In your shortcode, the content being outputted is being done so via echoing out which although is fine, however in order to maintain call stack priority from the theme, I'd recommend returning content inside your shortcode rather than echoing it out onto your page.
You also might want to look into wrapping your $content variable around some sort of a content sanitization method like sanitize_text_field to ensure that no additional markup is being added to the page from the where the shortcode is being used, and that whitespaces are also removed.
https://developer.wordpress.org/reference/functions/sanitize_text_field/
Another thing you can do is re-work your shortcode such that you have an attribute which is the heading text of the shortcode. This can then be sent in as a parameter to your shortcode function, and if you're echoing content into the page you would first do so by outputting the header, followed by the content which wraps those <pre> tags.

Post name and custom fields in Wordpress 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.

Can I change the contents of a description meta tag generated by Yoast in a WordPress site?

I am quite new to WordPress so I am not sure how this would work.
Essentially I have the Yoast plugin handling my SEO related things. The main one giving problems is the description meta tag.
The site is bilingual and I would like the description meta tag to be translated as well. Yoast currently does not provide that option, unless I get another plugin which I do not want to get into.
As it stands, I am told that I can insert the description meta tag through the functions.php using add_action. This unfortunately does not work as it only adds another description meta tag.
Currently my code looks like this:
function insert_meta_tag_in_head () {
echo '<meta name="description" content="My New content" />';
}
add_action('wp_head', 'insert_meta_tag_in_head', 1);
So basically this just gives me a second description meta tag. I have also seen in other threads, that if I want to replace a tag, I should use the do_action function. Which I call as follows:
do_action('wp_head', 'insert_meta_tag_in_head');
This however does nothing.
What am I doing wrong? How can I change the contents of the description tag given to me through Yoast?
We've previously changed a Yoast title before, however, a description is something we have been working on too.
To sum-up the link above:
You need to filter the wpseo_metadesc action in order to display your
new description.
add_filter('wpseo_metadesc', 'new_desc_function');

How to add inline css in WordPress plugin/theme generated by shortcode in page content?

I have a plugin and I need to correctly include inline css in WordPress using wp_add_inline_style() function. There is not problem to include CSS using this function in plugin, it works fine. But I have shortcodes that generates Custom CSS (different for different shortcode). For example I have shortcode that add text title with custom color to page (I generated unique CSS class name for this title DIV and add corresponding styles).
For example:
[mytitle text="Title example" color="#666666"]
And I have css that I need to inline:
.mytitle-id-4324324 h1 { color:#666666; }
As you see I can have multiple shortcodes on the same page, with different colors inside. The problem that if I use wp_add_inline_style function in shortcode it never not work. It should be inlined in "wp_enqueue_scripts" action, that does not triggered in right time from shortcode as I understand (because shortcodes loaded by WordPress AFTER wp_enqueue_scripts, when WordPress generate content). How to resolve this?
Note: I know that I can add inline CSS in HTML code or echo and it works, but I does not need this. My question how to get this work with wp_enqueue_scripts()?
As I understand I need to get one big global custom CSS from entire page shortcodes somehow, and then send it to some function that will include this big custom css for this page. But how to do this because shortcodes defenitions executed after including CSS by WordPress?
Example code for my shortcode:
function mgt_shortcode_header_block_wp($atts, $sc_content = null) {
.. some code here where shortcode show HTML output and generate custom css
}
add_shortcode("mgt_header_block_wp", "mgt_shortcode_header_block_wp");
How I can add inline custom css from shortcode? I can't do this inside shortcode (because its will not be called in wp_enqueue_scripts action) and I can't add some function after this function, because - custom css variable available only inside shortcode (I can't make it global var, because this is not good to use global vars here), I understand that I may need to pass it to some function from shortcode, something like:
function mgt_shortcode_header_block_wp($atts, $sc_content = null) {
.. some code here where shortcode show HTML output and generate custom css
mgt_add_to_inline_styles($custom_css); // how should work this function to correctly add inline styles passed to it?
}
add_shortcode("mgt_header_block_wp", "mgt_shortcode_header_block_wp");
But this will not work, because mgt_add_to_inline_styles() function will be executed only when this shortcode will be used on page in content, and this will be AFTER wp_enqueue_scripts action in any case, even if I will try to add mgt_add_to_inline_styles() to wp_enqueue_scripts action somewhere somehow.
So from my example I does not understand what code should be inside mgt_add_to_inline_styles() function to make it work correctly?

Categories