WordPress shortcode fails in Custom theme - php

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().

Related

Is there a content output wrapper in wordpress instead of using echo?

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

Is it possible to check rendered page content in wp_enqueu_scripts?

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.

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.

Wordpress shortcode with separate template/file?

I'm at an early stage of learning Wordpress (and shortcode), so bear with me:
To me, shortcodes seem like a swiss army knife of not having to use page-specific templates for everything. I like to build as many pages in the wysiwyg as possible, but often I would need some (reusable) php stuff for displaying stuff in a certain way.
Having googled a lot, it seems to me the way to do shortcodes is like:
function caption_shortcode( $atts, $content = null ) {
return '<span class="caption">' . $content . '</span>';
}
My question is, is it possible to put the html in a separate template-ish file? It seems wrong and verbose to put all this markup here, escape quotes, et.c. Like a template-file for a shortcode, to which the shortcode can pass some Data Transfer Object (or simply just some scoped variables). So: display in template-file, logic for finding data (to pass to said template-file) in shortcode-function (wherever it may be defined, functions.php, separate plugin, or something).
You can set-up views(php files) and then include partial views into those ones. Wordpress allows templates to be includes within other templates to ensure code reuse and its easily modifiable by child themes. You can use this function to include those
get_template_part( $slug );
However, in your case, the short code function needs to return the value to the caller function. So, this setup will not work.
For code that effects FUNCTIONALITY, put your code in a plugin.
For APPEARANCE, put your code in your theme's template files or funtions.php file.
Many beggining WP developers lump all their code into the theme's functions.php file, this is often the wrong place for it (if that code might ever get exported to another theme, for instance). Only put code specific to a specific theme in a theme's functions.php .
To get Wordpress to recognize your plugin, create a php file and start the file like this:
<?php
/*
Plugin Name: My Caption Shortcode Plugin
Description: A really cool plugin
*/
function caption_shortcode( $atts, $content = null ) {
return '<span class="caption">' . $content . '</span>';
}
?>
Put this file in your plugins directory (usually, you should create a sub directory for each plugin). Plugins are usually held in /wp-content/plugins/ . Then you can activate or deactive the code as a plugin, when you go to the plugins tab in the admin menu.
Of course, this plugin won't do anything as is. Remember that plugin functionality should be hooked into Wordpress via action hooks, filters, and shortcodes. For a shortcode for instance, you'd use the function add_shortcode somewhere to let Wordpress know your function is a shortcode.

the_content filter doesn't return html code, any substitute?

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.

Categories