Execute a WordPress shortcode in Genesis breadcrumbs - php

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.

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

Call first word of post title in page template

I want to do exactly what is talked about in this question: Get first word in string php the_title WordPress
(I think) I understand that putting this code in functions will trim the title, but how do I actually call it in the page template?
In Wordpress one does not call specialized functions in the template to modify such things as the title (or other data from the database).
Instead Wordpress offers filters to modify the output. See:
https://codex.wordpress.org/Plugin_API/Filter_Reference/the_title
Example: (in functions.php or some related PHP file from the used theme)
function title_first_word( $title, $id = null ) {
/* optional: check here on which page you are or which template is in use */
$titleParts = explode(' ', $title);
return $titleParts[0];
}
add_filter( 'the_title', 'title_first_word', 10, 2 );

WordPress shortcode fails in Custom theme

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

WooCommerce: Single-product-postslug template not working?

First,I know wordpress won't allow you to use template for posts (WooCommerce's product page is built via post). So I look for the Template Hierarchy.
It says there:
single-{post-type}-{slug}.php (Since 4.4). First, WordPress looks for a template for the specific post.
For example, if post type is product and the post slug is dmc-12, WordPress would look for single-product-dmc-12.php.
single-{post-type}.php – If the post type is product, WordPress would look for single-product.php.
single.php – WordPress then falls back to single.php.
singular.php – Then it falls back to singular.php.
index.php – Finally, as mentioned above, WordPress ultimately falls back to index.php.
So I created a template and name it single-product-ccc (ccc is one of my product's slug name), but nothing happened, nothing was affected.
But by creating a template named single-product will affect all of the product pages.
Why is that happening?
I don't get it. Even a single-2313.php (2313 is one post's id) will overwrite the default single.php for that 2313 post.
Why single-product-slug is not working in the same way?
Thanks.
Let me first correct you at one point. You say: "WooCommerce's product page is built via post".
This is not correct. WooCommerce creates a new post type 'product'. You can see all custom posts types that WooCommerce creates here: https://docs.woocommerce.com/document/installed-taxonomies-post-types/
The 'product' post type uses the template single-product.php.
As you say, WordPress Template Hierarchy Docs say that you can create a template for a particular product:
single-{post-type}-{slug}.php (Since 4.4). First, WordPress looks for a template for the specific post. For example, if post type is product and the post slug is dmc-12, WordPress would look for single-product-dmc-12.php.
WooCommerce is a WordPress plugin and as so, it doesn't always follow WordPress rules strictly, and this is one of those cases. But you can use a filter to correct this very easily. In your theme's functions.php simply add:
add_filter( 'template_include', 'custom_single_product_template_include', 50, 1 );
function custom_single_product_template_include( $template ) {
if ( is_singular('product') && (get_the_ID()==30)) {
$template = get_stylesheet_directory() . '/woocommerce/single-product-30.php';
}
return $template;
}
where get_the_ID() is the id of your Product. Now, you can create a new template as for example single-product-30.php
Try adding this in functions.php
function your_theme_add_woocommerce_support() {
add_theme_support( 'woocommerce' );
}
add_action( 'after_setup_theme', 'your_theme_add_woocommerce_support' );
and now onwards, you can use woocommerce/single-product.php for customisation

Genesis and Custom Meta Box

I have been using WPAlchemy for my custom meta boxes. I can usually get the meat box values to display using something like <?php $custom_mb->the_value('summary'); ?> but I am having trouble displaying the data in a genesis child theme. If use the above example I can get the post to display but it is at the very top of the page, even above the header. So I attempted to hook into the genesis_post_content hook using this
add_action('genesis_post_content', 'meta_content');
function meta_content() {
echo "Hello World";
}
I can echo Hello World this way but receive errors when trying the first example in the function. Any help would be greatly appreciated.
I do not know Genesis and I do not use WPAlchemy ( I avoid "frameworks" like fire ) but my logic dictates that if you can see the value at the top of the page like you described ( above header ) than the method the_value() is doing a direct echo, where you would need a return value.
The internal wordpress core logic dictates that whenever you have an echo function ( e.g. the_title() ) you would potentially have an equivalent return function ( e.g. get_the_title() ) and it would get the same function name with an added get_ prefix.
If the same wordpress logic is applied to those "frameworks" , or in this case to the WPAlchemy class , so instead of
$custom_mb->the_value('summary'); // if this is direct echo
you should be able to do :
$custom_mb->get_the_value('summary'); // then this should be return
Note that I did not tested it ( not using those "frameworks" , remember ?? ) but if it is indeed the case with WPAlchemy , than you would not need to invoke the genesis filter ( which by itself seems a bit wrong becasue the_content filter should be about the_content and not about meta_data but not knowing genesis I can not really say )

Categories