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

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

Related

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.

Execute a WordPress shortcode in Genesis breadcrumbs

I've created a shortcode [year] to show the current year dynamically. I enabled it in the post title by using this filter:
add_filter( 'the_title', 'do_shortcode' );
Shortcode executes successfully in the post title but displays as it is in the breadcrumbs. I'm using Genesis Framework so is there any way to make it work in the Genesis breadcrumbs too?
Using the_title filter like you have done, should actually work just fine (I just tested and confirmed it's working on a local site of mine).
As a matter of fact it can sometimes be a problematic filter because people expect it to filter just the page title, but in reality it filters the_title whenever it's pulled from the database, including the Page Title, Breadcrumbs, Menu Item Name, etc.
That said, if it's not working for some reason, Genesis has a handy Genesis_Breadcrumb class. Most notably it has the genesis_breadcrumb_args filter that lets you remove the "You are here:", etc. Provided the theme you're using is implementing Genesis Breadcrumbs and not some custom ones, you actually have access to the lesser utilized genesis_build_crumbs filter.
The build_crumbs() method returns an array of Page/Post Names that you can modify using a simple foreach loop or array_map.
Take the following function:
function so52299149_breadcrumbs( $crumbs ) {
$new_crumbs = array_map( function($val){
return do_shortcode( $val );
}, $crumbs);
return $new_crumbs;
}
add_filter( 'genesis_build_crumbs', 'so52299149_breadcrumbs' );
What it will do is run each element in the $crumbs array through the do_shortcode() function, giving you the desired output. Again, the way you have it, add_filter( 'the_title', 'do_shortcode' ); should be working - so check how the Child Theme you're using is implementing breadcrumbs, especially if the function above doesn't work - if that's the case there's likely something else at play.

Creating a shortcode from a backend page

I am trying to create a shortcode from a page that currently resides in the back end. The page has several acf fields as part of a form that creates a request. I would now like to have the same page on the front end. I have tried following the syntax of creating a shortcode from a function after reading about shortocdes, its api and doc and several different tuts online.
add_shortcode('create_requests', array($this, 'load_custom_wp_admin_style'));
^ The attempt above didn't work and I don't get any output when I include the shortcode in a new page.
You can notice that the function I am trying to use 'load_custom_wp_admin_style' returns a null value and uses hooks.
This is the file that contains the function.
Try to include file like below code. I checked your file according to me you need use the plugin url it seems like you are developing the plugin
wp_register_style('your_namespace', plugins_url('style.css',__FILE__ ));
wp_enqueue_style('your_namespace');
wp_register_script( 'your_namespace', plugins_url('your_script.js',__FILE__ ));
wp_enqueue_script('your_namespace');
Assuming that the page you want to display on the front end is a normal WordPress page - created in the pages tab, post type page.
Very simply you can just use the following PHP code to include it in a template:
<?php
$page = get_post(192994);
echo $page->post_content;
?>
If it needs to be a shortcode you can add this into your functions.php:
function output_page_function($atts) {
$page_id = $atts['page_id'];
if (!$page_id) return false;
$page = get_post($page_id);
return $page->post_content;
}
add_shortcode('output_page', 'output_page_function');
And include a shortcode where desired (with 'page_id' attribute)
[output_page page_id=192994]
If it's not a WordPress page, but an actual wp-admin screen, then this would be significantly more difficult/not possible.

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.

How to make Wordpress Short-code for recent posts

I want to create a simple Wordpress shortcode for recent posts to use it inside a post.
I know the basic steps of creating a shortcode:
1- I open the WP theme "functions.php" file and include a new custom file created in same directory i name it "custom-shortcode.php"
2- Inside the "custom-shortcode.php" i write the actual code:
<?php
// Shortcode function
function custom_shortcode() {
// Use WP recent posts function with default values return
return php wp_get_recent_posts( $args, $output );
}
// Add shortcode (WP function)
add_shortcode( 'recent-post', 'custom_shortcode' );
?>
I want to use the wp default function for getting recent posts, is my code will work?
I want to get shortcode with minimal code and with default values return so i can focus on learning how can i make my own shortcode.
Use
shortcode_atts(array( for make your custom style..
You can follow , http://codex.wordpress.org/Shortcode_API

Categories