Add shortcode child theme WordPress - php

I work in multisite
I have a child theme of my parent theme to customize a page template.
In this customize page template I would like to display a shortcode, which allows to retrieve the url address of the site in question.
My function in my child theme file, function.php:
add_action( 'init', function() {
add_shortcode( 'site_url', function( $atts = null, $content = null ) {
return site_url();
} );
} );
I would like to declare this shortcode in my template customizer, like this:
/*
Template name: Test
*/
get_header(); ?>
<div>
<h1>Notre site web : [site_url]</h1>
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
get_template_part( 'content', 'page' );
endwhile;
endif;
?>
</div>
<?php get_footer(); ?>
````but the shotcode doesn't generate anything at all in my customize template.
I must have missed something... :-(
Can you help me ?
Thanks

This is because shortcodes that are hardcoded into templates don't get rendered by default. What you want to do is put the shortcode in the function do_shortcode() and then it should output correctly.
echo do_shortcode('[site_url]');

Related

i can not access the post in custom template of my wordpress plugin

i added following code to use custom template for my recipes single page
function override_single_template( $single_template ){
global $post;
if ($post->post_type == "recipes"){
$single_template = plugins_url('/recipe-single-page-template.php',__FILE__);
}
return $single_template;
}
add_filter( 'single_template', 'override_single_template',10);
and in my template i added following code
<?php
/*
Template Name: recipe-single-page-template
Template Post Type: recipes
*/
require_once("../../../wp-load.php");
?>
<?php get_header(); ?>
<?php echo $post->ID?>
<?php get_footer(); ?>
but i do not access the post and echo out post id will cuses the following error
Trying to get property of non-object
var dump $post outputs null
NULL
and following code will print out my custom template address
$current_url = "https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo $current_url;
top code result:
https://charter.test/wp-content/plugins/recipe-plugin/templates/single-recipes.php
now whate should i do?
Are you trying to redirect a custom post type called recipes to a custom template? You don't need to override, by default wordpress has a built in dynamic content display system.
In your case, upon displaying a recipe it will search first for single-recipe.php, if not found, fallback on single.php, if not found, fallback on 404.php and finally on index.php
You just have to create a file called single-recipe.php.
Your second problem is that there is no loop displayed in your file, you have to tell to wordpress that if a post exist, it should retrieve it and present it to you. For that we use the loop system.
Your single-recipe.php file should look like something like this:
<?php
/**
* Get theme header
*/
get_header();
/**
* Start loop
*/
if ( have_posts() ):
while ( have_posts() ):
the_post();
/**
* If we find a post, output the tilte
*/
echo the_title().'<br/>';
/**
* End loop
*/
endwhile;
endif;
/**
* Get theme footer
*/
get_footer(); ?>

Unable to inject a specific classname to body tag when a specific WordPress page template loads

I have a wordpress website in which there is a custom theme where I can create template parts for each webpage. I want to include/inject a specific class (ex: abc ) to the body tag in header.php when a specific page template (ex: abc.php) loads.
First I used the code bellow in header.php it did not work.
<body <?php if ( is_page_template( 'template-parts/page/abc.php' ) ) { body_class( 'abc' ); } else { body_class(); } ?>>
Then I added bellow code to functions.php but it is not working either.
add_filter( 'body_class','abc_class' );
function abc_class( $classes ) {
if ( is_page_template( 'template-parts/page/abc.php' ) ) {
$classes[] = 'abc';
}
return $classes;
}
I do not understand what I am doing wrong here. How can I fix this?
First of all the is_page_template() function works for WP templates, I mean which have in the beginning of the file
/*
* Template name: Your Super Template
*/
So, the function returns true is a page with this template is actually displaying. I will tell you more, you even do not have to add custom body classes because function body_class() adds specific unique classes for each page template :)
Second, the page templates can be in the theme directory itself or in 1-level subdirectory, so
<body <?php if ( is_page_template( 'abc.php' ) ) { body_class( 'abc' ); } else { body_class(); } ?>>
or
<body <?php if ( is_page_template( 'page-templates/abc.php' ) ) { body_class( 'abc' ); } else { body_class(); } ?>>
Your template in 2-level sub-directory, so it is not actually a Page Template, it is a template part.
So, in two words, the function is_page_template() is for Page Templates but not for template parts
I was able to add the following in functions.php so that it takes the slug and inject that to the body class every time a specific page loads.
add_filter( 'body_class', function( $classes ) {
if ( is_page() ){
$slug = get_queried_object()->post_name;
return array_merge( $classes, array( $slug ) );
}
return $classes;
} );

Use a template file from a plugin in WordPress

I try to develop a plugin which includes a file from a template. At this moment my code view is like this:
/* Add a query var for template select, and and endpoint that sets that query var */
add_action( 'init', 'wpse22543_rewrite_system' );
function wpse22543_rewrite_system() {
global $wp, $wp_rewrite;
$wp->add_query_var( 'template' );
add_rewrite_endpoint( 'kalkulator_leasingowy', EP_ROOT );
$wp_rewrite->add_rule( '^/kalkulator_leasingowy/?$',
'index.php?template=kalkulator_leasingowy', 'bottom' );
$wp_rewrite->flush_rules();
}
/* Handle template redirect according the template being queried. */
add_action( 'template_redirect', 'wpse22543_select_template' );
function wpse22543_select_template() {
global $wp;
$template = $wp->query_vars;
if ( array_key_exists( 'template', $template ) &&
'kalkulator_leasingowy' == $template['template'] ) {
global $wp_query;
$wp_query->set( 'is_404', false );
include( get_stylesheet_directory().'/kalkulator_leasingowy.php' );
exit;
}
}
function prefix_movie_rewrite_rule() {
add_rewrite_rule( 'kalkulator_leasingowy', 'index.php?template=kalkulator_leasingowy', 'top' );
}
add_action( 'init', 'prefix_movie_rewrite_rule' );
This code runs very fine and includes the template file, but my template (header.php and footer.php) by default uses a Visual Composer and when I use this code on a page, view this:
Visual Composer works good on all pages without a /kalkulator_leasingowy.
How I can include a VC into /kalkulator_leasingowy as well?
File kalkulator_leasingowy.php
<?php
get_header();
?>
<div class="main-wrapper">
<div class="container ">
<div class="row">
</div>
</div>
</div>
<?php get_footer(); ?>
I'm not really understanding where you are trying to render custom Visual Composer code since your template file doesn't have any in it.
But based on your edit, it looks like you might actually want to be using a child theme. These make it very easy to add new template files to the parent theme without editing any of the parent's code and eliminate the need for most of your complex code.
If perhaps you are injecting the Visual Composer code from somewhere else, make sure you are applying the content filters rather than just inserting or echoing to the front end.
$content = '<div id="my_custom_content">[vc shortcode contents here]</div>';
echo apply_filters('the_content', $content);
This will make sure the end content is filtered and rendered appropriately. You might read this related answer for more information.

Page not showing after add a function on child theme

I am using Genesis framework and modern-studio-pro as a child theme. I have just copy this code
function genesis_standard_loop() {
//* Use old loop hook structure if not supporting HTML5
if ( ! genesis_html5() ) {
genesis_legacy_loop();
return;
}
if ( have_posts() ) :
do_action( 'genesis_before_while' );
while ( have_posts() ) : the_post();
do_action( 'genesis_before_entry' );
printf( '<article %s>', genesis_attr( 'entry' ) );
do_action( 'genesis_entry_header' );
do_action( 'genesis_before_entry_content' );
printf( '<div %s>', genesis_attr( 'entry-content' ) );
do_action( 'genesis_entry_content' );
echo '</div>';
do_action( 'genesis_after_entry_content' );
do_action( 'genesis_entry_footer' );
echo '</article>';
do_action( 'genesis_after_entry' );
endwhile; //* end of one post
do_action( 'genesis_after_endwhile' );
else : //* if no posts exist
do_action( 'genesis_loop_else' );
endif; //* end loop
}
from Genesis to child theme functions.php. After that my site is showing
Fatal error: Cannot redeclare genesis_standard_loop() (previously declared in /home/u282646374/public_html/wp-content/themes/modern-studio-pro/functions.php:198) in /home/u282646374/public_html/wp-content/themes/genesis/lib/structure/loops.php on line 115
I have delete that code from child theme but site not showing. Please tell me how can i regain my site? This is the link of my page http://andrewspalding.co.uk/
Your problem is that you are redeclaring the function genesis_standard_loop() inside functions.php of your child theme while on the main genesis theme this function is inside lib/structure/loops.php child themes will overwrite the parent theme functions if they are inside the same file that they are in the parent theme, if you want to modify a function from the parent theme you might contemplate the idea of looking for filters or actions.
If you MUST redeclare the function try recreating the same file structure, but remember to add all of the other functions inside that particular file.

Trying to add two different strings in PHP

I came up with an solution for not showing an sidebar in Wordpress when this is not filled with widgets. I did this by calling for the slug from the current page and named my sidebars the same as the slugs from my pages.
I use the Custom Sidebars plugin to add sidebars. This plugin adds 'cs-' in the id of a sidebar before the name of the sidebar. So for example I've named my sidebar 'Test' than the id of the sidebar will be 'cs-test'.
Now I want my php code to check if the sidebar is named the same as the slug from the page. So I called the_slug and before the slug I want to add cs-. With some help I came up with this:
<?php if ( is_active_sidebar( 'cs-'.the_slug(false) ) ) { ?>
<?php dynamic_sidebar( is_active_sidebar( 'cs-'.the_slug(false) ) ) ?>
<?php } else { ?>
<?php } ?>
But that doesn't seems to help. Any thoughts how to fix this? Thanks already!
Your are concatenating a string cs- with a function the_slug(false). This might cause the error, depending on what the function is returning.
Solution 1:
You could call and save the function beforehand and concate two vars:
<?php
$slug = the_slug(false); //Get return value
$slug = (string)$slug; //Force string conversion
$slug = 'cs-'.$slug; //Concate
if ( is_active_sidebar( $slug ) ) { ?>
<?php dynamic_sidebar( is_active_sidebar( $slug ) ) ?>
<?php } else { ?>
<?php } ?>
.
Solution 2:
If you need this type of functionality often try to add a string parameter to the the_slug() function (should be a custom function in your functions.php) and do the concatenating within the function itself:
function the_slug($boolen, $string='') {
//Your code
return $string.$your_var;
}
Your sidebar code would look like this:
<?php if ( is_active_sidebar( the_slug(false, 'cs-') ) ) { ?>
<?php dynamic_sidebar( is_active_sidebar( the_slug(false, 'cs-') ) ) ?>
<?php } else { ?>
<?php } ?>

Categories