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.
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 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.
I'm creating a theme and i want the user to be able to use shortcodes.
Right now it outputs [the_shortcode] and I think I know why but don't know how to fix it.
I load the content of the page not in the conventional way.
Preferably the way is to load the_content() function. But the way my template is designed it loads content based upon placement in the hierarchy of pages.
So a parent has a different look then a child.
To do this I load the content with a foreach loop and echo out $grandchild->post_title. The page being a grandchild of a parent.
Now the way to fix this, according to the internet, is to use the apply_filters() function.
The function expects two parameters and I have no clue on how to fill them:
function apply_filters( $tag, $value )
This is my function for the shortcode:
function output_function(){
return 'Knees weak, arms are heavy';
}
add_shortcode('output', 'output_function');
The shortcode is placed in a page post like this: ['output']
Any thoughts on how to output page content through the filter?
What you want is the_content
$content = 'some string that has a [output] shortcode';
echo apply_filters('the_content', $content);
This filter will make sure all $content is parsed like the WordPress editor.
The same like the_content().
Preface: I have developed a plugin, so editing theme files is out of the question.
My shortcode function:
/*** [leaderboard] shortcode with tournament_id input value ***/
function leaderboard_shortcode( $tournamentid ) {
ob_start();
include_once dirname( __FILE__ ) . '/leaderboard.php';
$output = ob_get_clean();
return $output;
}
add_shortcode( 'golf-deputy-leaderboard', 'leaderboard_shortcode' );
I tried adding in to relevant pages, however, if the shortcode is added to a page not controlled by the plugin - e.g. the home page - , the shortcode caches.
I guess my question is this: what options do I have to NOT cache the content of the shortcode, even if the page it is placed on, has caching enabled.
I have been racking my brain, the internetz, the WordPress Codex... all to no avail. I am open to any and all suggestions. Help me Stackoverflow, you're my only hope.
Cache functions start working before all possible filers&actions, and as shortcodes run inside the_content filter, it is impossible not to cache a shortcode only. I mean, when WP returns cached data, it returns static HTML data, no any theme&plugin's PHP runs in that case.
You need add that page (which has a shortcode in its content) to exclude list of your plugin. All popular cache plugins have such exclude field inside their settings.
Or another way is building custom cache solution which simply ignores caching then the_content contains any shortcode.
I was working on a WordPress plugin, and I found that when the_content filter hook is used, it gives content of the post to the function as a parameter, but in plain-text format. I want to get the HTML content of the post, any way to achieve this?
Thanks
- Kapeel
Finally, after searching a lot, I had solved it.
As said by TheDeadMedic - "Are you sure that the post content does actually contain HTML? Don't forget WordPress will add paragraphs on-the-fly, and won't necessarily store them in the DB."
WordPress does by using a function called wpautop();
I just used this with get_the_content(); , and I got it working.
Here's an example of how you can achieve this -
function myPluginReplaceContent() {
$content = wpautop(get_the_content());
$content .= myPluginGetData(); // do whatever you want to - here
return $content;
}
EDIT :
I found that this function won't apply filters by other plugins. The following function won't cause any issues.
function myPluginReplaceContent($thecontent) {
$thecontent .= myPluginGetData(); // do whatever you want to - here
return $content;
}
Do you have any plugins installed, or are you filtering the_content elsewhere?
By default, the content passed through the filter the_content is pretty much what's in the database, minus a bit of parsing (handling <!-- more --> teasers etc.).
Are you sure that the post content does actually contain HTML? Don't forget WordPress will add paragraphs on-the-fly, and won't necessarily store them in the DB.