PHP include via shortcode is moved by WP Gutenberg Editor - php

I'd like to include a specific php-file into a WP post. Therefore, I'm using the default WP Gutenberg editor. In the middle of the post, I inserted a shortcode like [my-php-file]. In my functions.php I used the following code:
function include_file ( $atts ) {
include('file.php');
}
add_shortcode( 'my-php-file', 'include_file')
So far, so good. The content of my php-file is loaded, but on the top of the WP post and not in the middle, where the shortcode is placed.
Can anybody tell me please why this is happening and how I can fix this issue? So that the php-file will be displayed and included in the middle of the post?

The add_shortcode callback expects you to return content, by including the file it will execute the code and output the content to the output buffer (rather than as the function result).
You can try using output buffering to return content rather than outputting it straight into display, as in:
function include_file ( $atts ) {
ob_start();
include('file.php');
return ob_get_clean();
}
add_shortcode( 'my-php-file', 'include_file')`
You can find more information on the WordPress Documentation at https://developer.wordpress.org/reference/functions/add_shortcode/#more-information :
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 unexpected
side effects from the call since you cannot control when and where
they are called from.

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

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.

Put php logic function inside wordpress Shortcode

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]' );

PHP inside shortcode function in Wordpress

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.

How to display result of PHP render in HTML ID

After digging internet for an answer I cannot find any idea about how to deal with my issue. I think the problem is common for someone who knows PHP a little bit.
To describe the situation. For some custom WordPress plugin I've got two PHP files: ff_config.php and loantest_form.php. First file contains some configurations of plugin plus following lines:
/**--------------------------TABLE SHORTCODES-----------------------*/
function render_loantest_form() {
include(plugin_dir_path(__FILE__) . 'front/loantest_form.php');
}
add_shortcode( 'render_loantest_form' , render_loantest_form );
/**--------------------------DISPLAY PLUGIN IN FOOTER-----------------------*/
add_action('wp_footer', 'display_loantest');
function display_loantest() {
echo render_loantest_form();
}
Which I suppose rendering second file containing enqueue scripts (js/css) and whole HTML output and placing in wp_footer where it exactly is on my page.
The question is: how to change mentioned lines to allow me to place render result (loantest_form.php) in specific div / id on page (for example #sidebar-slider)?
If you want to display your shortcode in a template file,
echo do_shortcode('[render_loantest_form]');
Enable the use of shortcodes in text widgets.
add_filter( 'widget_text', 'do_shortcode' );
And inside the text editor
[render_loantest_form]
You can read more about do_shortcode()
Note that you need to be sure that the file load in the render function is good and that it returns the expect content.

Categories