How to use shortcodes inside metaboxes in WordPress? - php

I've created a custom metabox using this example. When the admin-user enters a shortcode in my metabox, the shortcode displays on my front-end as plain text such as:
[menu]
How can I make sure WordPress parses the shortcode so an actual menu is outputted?
I'm using this in my page.php template to display the contents of my custom metabox:
echo get_post_meta( get_the_ID(), '_my_custom_meta_value_key', true );

You can simply use do_shortcode provided by WordPress. This can be used to manually search through the given string for shortcodes.
According to docs for do_shortcode:
Searches content for shortcodes and filters shortcodes through their hooks.
This is how you can do it:
$shortcode = get_post_meta( get_the_ID(), '_my_custom_meta_value_key', true );
echo do_shortcode($shortcode);

Related

WordPress Shortcode generated Shortcode is not applied (Pods, Leaflet)

I want to output a list of locations as a table and mark them up on a map. For this I have created a content type in WordPress with Pods with a property "Address". With a Pods shortcode, I am able to have the table created and list all the entries. With another Pods shortcode, I am able to generate a Leaflet shortcode.
The problem is that the Leaflet shortcode is output to the frontend, but is not interpreted by Leaflet as a map. If I use the Leaflet shortcode generated by Pods from the frontend in the backend, the map works as desired.
I'm afraid the problem arises because the shortcode generated by a shortcode is not taken into account and implemented.
I have created a WordPress page and noted the following shortcode:
[pods name="locations"]<br /><br />[/pods][pods name="locations"]<br />[leaflet-marker address="{#locations-address}"]<br />[/pods]
A correct shortcode is output in the frontend:
[leaflet-marker address="Ingolstädter Str. 101, 80939 München"]
In consultation with the developer of the Leaflet plugin for WordPress, I tried to insert the function with XYZ PHP code:
<?php
$a = do_shortcode( '[pods name="locations"][leaflet-marker]' . '[' . 'leaflet-marker address={#locations-address}' . '&#093' . '<br>[/pods]');
echo "[leaflet-map] $a [leaflet-marker]";
?>
The result is better, as the map is generated largely as desired, but when I insert the XYZ PHP code into the WordPress page, no map is output.
By default including a shortcode within a Pods shortcode will not work. If you need this functionality to work, you need to include the following constant in your wp_config.php file:
define('PODS_SHORTCODE_ALLOW_SUB_SHORTCODES',true);
Then, if you need to parse a shortcode within the Pods template, custom template or within your Pods shortcode, just add shortcodes=1 to the Pods shortcode.
Or you can use the filter below to have it always enabled:
add_filter( 'pods_shortcode', function( $tags ) {
$tags[ 'shortcodes' ] = true;
return $tags;
});
All these are explained at Pod's documentation.
I have found a solution in the meantime. The keyword is nested shortcodes (https://codex.wordpress.org/Shortcode_API) and the function "do_shortcode()".
Using a custom WordPress plug-in to run a script and output it as a custom shortcode, I was able to generate the map as desired. For this I used the following code. It is important to note that "do_shortcode()" must be used twice, first for Pods and then for Leaflet.
function map_func( $atts ){
$leaflet_map_shortcode = "[leaflet-map lat=48.13391418715657 lng=11.582841421510087 zoom=15 fitbounds max_zoom=19]";
$leaflet_map = do_shortcode($leaflet_map_shortcode);
$placese_marks_shortcode = '[pods name="places"]<br />[leaflet-marker address="{#places-address}" [...] [/leaflet-marker]<br />[/pods]';
$placese_marks = do_shortcode($placese_marks_shortcode);
$placese_marks2 = do_shortcode($placese_marks);
return $leaflet_map . $placese_marks2;
}
add_shortcode( 'map2', 'map_func' );

Display Product Image from WooCommerce plugin using a custom plugin

I'm trying to learn WordPress and developing custom plugins. I'm trying to make a custom plugin where it will display just the Product Image from the Woocommerce plugin to the page. I have 1 product, and I've been reading documentation and haven't really found anything that explains it clearly or works. This is all I have now
<?php
$gallery_shortcode = '[gallery id="' .intval($post->$post_parent).'"]';
print apply_filters('the_content', $gallery_shortcode);
?>
Honestly the question is not very clear, so I'll try to guess what you're looking for.
You can "get" a product image with this function:
woocommerce_get_product_thumbnail();
If you need it in a custom single product PHP template, simply:
echo woocommerce_get_product_thumbnail();
If you need it in a shortcode inside the single product page:
add_shortcode( 'product_image', 'bbloomer_product_image_shortcode' );
function bbloomer_product_reviews_shortcode() {
return woocommerce_get_product_thumbnail();
}
...and then use [product_image] shortcode to show the image

wpbakery elements shortcode implement in php

I have created a shortcode through wpbakery Add Element section. I am trying to apply this shortcode in PHP
Here is the shortcode
[vc_masonry_grid post_type="post" max_items="10" grid_id="vc_gid:1592135090064-3fb6b6dd-7144-2" taxonomies="16"]
Here is shortcode with PHP code which I have applied in PHP file but it's showing nothing.
I got this function from here
https://developer.wordpress.org/reference/functions/do_shortcode/
echo do_shortcode( '[vc_masonry_grid post_type="post" max_items="10" grid_id="vc_gid:1591334217992-a1326795-3e97-8" taxonomies="16"]' );

Wordpress - Rev Slider not replacing shortcode

So, Basically I have a Revolutionary Slider plugin installed and created one slider. I added the shortcode :
[rev_slider alias="home-original"]
on the homepage. But the slider is just not replacing with the shortcode. The shortcode is displaying as it is on the homepage. I also tried the do_shortcode() function in the header.php but the same occurs.
When I tried this
shortcode_exists('rev_slider')
It prints false.
I also tried this
$active_plugins = (array) get_option( 'active_plugins', array() );
And it shows the rev slider is active.
What more should I try?
I use this to manually add shortcode in PHP:
<?php echo do_shortcode( '[rev_slider alias="slideshow"]' ); ?>
Maybe you forgot to use echo

do_shortcode breaks formatting

Has anyone encountered this problem before? Directly calling the shortcode from WordPress and calling the shortcode via functions.php yields different results.
Refer to this
Image 1 has decent design since it was invoked directly from WordPress UI while Image 2 somehow messed up the design because it was invoked from php (functions.php).
Additional info:
Current theme is DIVI
Shortcode/Plugin used is TableMaster
To add further information, I have this line of shortcode in WordPress (as seen on image 2).
[get_blogs_sc]
And on my functions.php, I have this function,
function GetActiveBlogs($i)
{
$actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if (preg_match('/sites/',$actual_link))
{
$user_id = get_current_user_id();
if ($user_id > 0)
{
echo do_shortcode('[tablemaster buttons="true" datatables="true" class="black-header-gray-alternate-rows" sql="some select where user_id = '.user_id.'"]', true);
}
}
}
add_shortcode('get_blogs_sc', 'GetActiveBlogs');
But when I use the shortcode directly from WordPress:
[tablemaster buttons="true" datatables="true" class="black-header-gray-alternate-rows" sql="some select where user_id = 14]
the display on my page looks good (as seen on image 1)
My objective on putting the shortcode in php-layer is for me to be able to capture the logged-in user from WordPress.
Thanks.
it's hard to diagnose what the issue is only from images ! without what the code really says.
but let's know what difference between do_shortcode and add_shortcode
do_shortcode( string $content, bool $ignore_html = false )
Search content for shortcodes and filter shortcodes through their hooks.
it return (string) Content with shortcodes filtered out.
// Use shortcode in a PHP file (outside the post editor).
echo do_shortcode( '' );
for more information check link
while add_shortcode Adds a hook for a shortcode tag.
<?php add_shortcode( $tag , $func ); ?>
$tag
(string) (required) Shortcode tag to be searched in post content
Default: None
$func
(callable) (required) Hook to run when shortcode is found
Default: None
it return nothing but when you write shortcode tag in post area it apply the function of shortcode for more information check Link

Categories