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.
Related
Background
I use php CPT templates with ACF. Usually i get ACF variables and depending on these variables i use get_template_part() to display content. in other cases i just use echo to deliver content. So far, this is working out for years now.
Problem
A customer is using DigiMember. DigiMember is a membership plugIn that uses shortcodes to protect/hide part of the content. This works only if the standard content output is used. so my get_template_part() sections interpret shortcodes as expected but my simple echo output sections do not and are not protected. i assume that digimember hooks itself in the main content function/loop in order to filter allowed content.
Shortcodes
So far i successfully testet the do_shortcode() php function with the digimember shortcodes. anything within is protected. My idea was to collect all output in a variable with ob_start and then output this variable within the do_shortcode() function, but this seems a bit off?!?
My question
Is there some kind of wrapper function in wordpress to output html/text instead of using simple echo? so that any string or ob_start buffer will be processed with all usual wordpress filters. ... or is my brain on a wrong path?
Thanks for your suggestions :)
You could try doing something like:
$return = '<div>';
$return .= do_shortcode('[yourshortcode]');
$return .= '</div>';
echo $return;
or
return $return // if your using the add_shortcode('short code function
I am trying to optimize asset loading of a plugin that I do not own. The plugin adds content to pages via shortcodes that may or may not contain conditionals that I am after. I need to somehow get the content fully rendered inside wp_enqueu_scripts and do regex to determine if assets should be loaded. Is this possible?
So far I have tried:
get_the_content() - only shows unrendered names of shortcodes.
the_content filter hook - runs after wp_enqueu_scripts so does not work.
the_content() function - actually echoes the content which is no good for just a check.
The official way to get the rendered content is applying the the_content filter to the content. This way:
$id = 0;
$content = get_the_content($id);
$content = apply_filters('the_content', $content);
All the filters that are registered will run, so also the shortcodes that have already been added. In case these are not added yet, you should revert to using javascript on the frontend to tackle the problem.
I have this code that displays an content to to users who have an paid subscribtion:
echo do_shortcode('[ihc-hide-content ihc_mb_who="5,7"] MY CONTENT HERE [/ihc-hide-content])
But I want to display inside this shortcode where it says MY CONTENT HERE, an logical function, like this:
if ( $a = $b ) { echo ($c); }
There is possibile to do this in some way?
P.S. I have made another shortcode, and put him inside like this:
do_shortcode('[ihc-hide-content ihc_mb_who="5,7"].do_shortcode('[my_shortcode]').[/ihc-hide-content])
But the 1st shortcode is not hide the content of 2nd shortcode.
Any help will be apreciated, ans sorry for my bad english!
If you're using an Enclosing Shortcode you should just be able to add the shortcode as a string to your do_shortcode() function. do_shortcode() will search the content that's passed to it and parse out any and all shortcodes that it's able to. Depending on how the ihc-hide-content shortcode is set up, it may not work quite properly, but you can get around that as well.
First and foremost, I would try just passing a single shortcode string to it:
do_shortcode( '[ihc-hide-content ihc_mb_who="5,7"][my_shortcode][/ihc-hide-content]' );
If that doesn't work (in all honesty, it should) - you can try and generate the output from my_shortcode into an Output Buffer like so:
// Turn on Output Buffering
ob_start();
// Output your shortcode
do_shortcode( '[my_shortcode]' );
// Turn off Output Buffering, and grab the content as a variable
$my_shortcode_output = ob_get_clean();
// Pass that complete output to the other shortcode
do_shortcode( '[ihc-hide-content ihc_mb_who="5,7"]'. $my_shortcode_output .'[/ihc-hide-content]' );
I'm using one of the plugins inside Wordpress which needs to be rendered with PHP because I want to use it as shortcode inside WPbakery.
Developer instructions for displaying in custom location is this line of code:
<?php wccf_print_product_field(array('key' => ‘mykey’)); ?>
I tried to add this in shortcode function like:
function newshortcode1() {
wccf_print_product_field(array('key' => ‘mykey’));
}
add_shortcode( 'newshortcode', 'newshortcode1' );
But sadly this doesn't work no matter how I change this code.
I'm using [newshortcode] insite post to display it.
Any suggestions what I'm doing wrong here?
You are using ‘ (apostrophes) instead of ' (Single quotes).
So, update from:
wccf_print_product_field(array('key' => ‘mykey‘));
to:
wccf_print_product_field(array('key' => 'mykey'));
Firstly a shortcode function needs to return the desired output (It essentially replaces the shortcode text in the content). IF wccf_print_product_field returned the desired output, then you could do
return wccf_print_product_field
HOWEVER I strongly suspect that wccf_print_product_field prints or echo's the output directly when called. So it would be doing it when wordpress calls apply_filters to the page content, NOT returning it to the shortcode text.
So if you really must use it in a shortcode (if there is no other function that just returns the product field), then you will have to trap the output so you can return it in the shortcode. Something like:
function newshortcode1() {
ob_start(); /* catch the output, so we can control where it appears in the text */
wccf_print_product_field(array('key' => ‘mykey’));
$output .= ob_get_clean();
return $output;
If you view source on the page with the shortcode as it is now, I'd bet that you see your product field somewhere earlier in the page where it shouldn't be, possibly right at the start.
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?