Wordpress - is_page_conditional - wordpress

I am struggling really hard with the is_page_template Conditional.
The problem is that when I "include" my template functions file (country_template_functions.php) into my functions.php file, the is_page_template conditional doesn't work anymore.
If I remove the "include(template...)" from my functions.php it does work again, but this is no solution since the file has to be included into the functions.php to make it work.
Can anyone explain me what might be wrong?
The is_page conditional works in both cases.
if (file_exists(THESIS_CUSTOM . '/country_template_functions.php')){
include(THESIS_CUSTOM . '/country_template_functions.php');
}
function test() {
if ( is_page_template( "country_template.php" ) ){
echo "is page template works";
}
}
add_action('thesis_hook_before_header','test');
I am using the Thesis Theme.
EDIT:
This seems to cause the problem but not sure exactly why. It causes that I cannot use the is_page_template after the thesis_hook_before_content_area
add_action('thesis_hook_before_content_area', 'country_page_template_swap_before_content');
function country_page_template_swap_before_content(){
if (is_page()) {
global $post;
global $swapped_post;
$page_template = get_post_meta($post->ID, '_wp_page_template', true);
if($page_template == 'country_template.php'){
update_post_meta($post->ID, '_wp_page_template', 'custom_template.php');
$swap_id = time();
update_post_meta($post->ID, '_country_template_swapped', $swap_id);
$swapped_post = $post;
}
else{
$template_swapped = get_post_meta($post->ID, '_country_template_swapped', true);
if(!empty($template_swapped)){
$swapped_post = $post;
}
}
}
}

Related

How to display Woocommerce category description with DIVI theme

I'm using DIVI theme and have created a custom category layout. However, I can't seem to find a way to add the category description to the layout. DIVI doesn't provide any answers, and I've searched here as well. Can I use some tags to get it?
The code below almost works - I do get the text, but not linebreaks, even though they are present if I do a echo '<pre>'; print_r($cat); echo '</pre>';
add_shortcode('cat_desc', 'cat_desc_shortcode');
function cat_desc_shortcode() {
global $wp_query;
$cat = $wp_query->get_queried_object();
if( $cat == null ) return;
$output = '<div class="page-description"> '.$cat->description.' </div> ';
return $output;
}
Thanks
Got it working now. For others in the same situation, add this code to your functions.php file in your (child) theme.
add_shortcode('cat_desc', 'cat_desc_shortcode');
function cat_desc_shortcode() {
global $wp_query;
$cat = $wp_query->get_queried_object();
if( $cat == null ) return;
$output = nl2br($cat->description);
return $output;
}
Then use the shortcode [cat_desc] in your layout, where you would like the category description to appear. This at least works for me.

Not getting the content of template made in plugin

I have created a template in my plugin with the help of this code
function ldc_add_thanku_page_template ($templates) {
$templates['thanku.php'] = 'Thanku Template';
return $templates;
}
function ldc_redirect_thanku_page_template ($template) {
if ('thanku.php' == basename ($template))
$template = PLUGIN_PATH .'templates/public/thanku.php';
return $template;
}
But when i am assigning this template to any page I am not getting the content inside it. For now i have added a very simple code, can anyone please help me with this.
<?php
/* Template Name: Thanku Template */
echo "<h1>Hello</h1>";
?>
Your Code will not work on the latest WordPress version, This code will work below 4.7
Try This
function ldc_add_thanku_page_template ($templates) {
$templates['thanku.php'] = 'Thanku Template';
return $templates;
}
function ldc_redirect_thanku_page_template ($template) {
$post = get_post();
$page_template = get_post_meta( $post->ID, '_wp_page_template', true );
if ('thanku.php' == basename ($page_template))
$template = WP_PLUGIN_DIR .'/mypluginname/thanku.php';
return $template;
}

Wordpress - First check page.php in plugin

Is it possible to make WordPress look at plugin directory first and if the system finds that page, for example, page.php, archive.php, single.php... show layout from plugin files and don't look at theme files?
Absolutely! Take a look at the single_template and archive_template filters. They're relatively straight forward to use:
function get_custom_post_type_template($single_template) {
global $post;
if ($post->post_type == 'my_post_type') {
$single_template = dirname( __FILE__ ) . '/post-type-template.php';
}
return $single_template;
}
add_filter( 'single_template', 'get_custom_post_type_template' );
There's also This Section on effectively adding single-post_type.php to the Template Hierarchy, but I prefer a slightly modified version that is IMO easier to read:
add_filter( 'single_template', 'my_single_templates' );
function my_single_templates( $single_template ){
global $post;
$file = '/my/path/to/templates/dir/'. $post->post_type .'/single-'. $post->post_type .'php';
if( file_exists( $file ) )
$single_template = $file;
return $single_template;
}

Relocate wordpress plugin output thats hooked into the content

I am trying to relocate the position of the output generated by a review plugin for wordpress. The plugin hooks into the end of "the_content". I have found the filter which works to remove the output:
remove_filter('the_content', array( EDD_Reviews::get_instance(), 'load_frontend'));
Now I am going crazy trying to relocate the output to a new location. I have created a custom hook in my theme file:
function reviews() {
do_action('reviews');
}
The above hook outputs just fine to custom location in the theme files when passed a simple function. However the tricky part is getting the reviews and review form to display. I tried adding it back with this:
add_action('reviews', array( EDD_Reviews::get_instance(), 'load_frontend'));
This did not work. The full function I am trying to call is below. Any body have any ideas how I may call this via my new hook? I am rather stuck.
public function load_frontend( $content ) {
global $post;
if ( $post && $post->post_type == 'download' && is_singular( 'download' ) && is_main_query() && ! post_password_required() ) {
ob_start();
edd_get_template_part( 'reviews' );
if ( get_option( 'thread_comments' ) ) {
edd_get_template_part( 'reviews-reply' );
}
$content .= ob_get_contents();
ob_end_clean();
}
return $content;
}
Many thanks in advance.
Ok mostly figured this out after studying classes. If anyone else is looking to do this then remove the hook as below:
remove_filter('the_content', array( EDD_Reviews::get_instance(), 'load_frontend'));
The following line will then render the reviews and form anywhere you like:
<?php echo EDD_Reviews()->load_frontend( '' ); ?>

How to conditionally check a custom single post template in WordPress

I am using the following function to call a custom single post overriding the default one single.php
function call_different_single_post($single_template)
{
global $post;
$path = get_stylesheet_directory() . '/includes/templates' . '/';
$single_template = $path. 'single-1.php';
return $single_template;
}
add_filter('single_template', 'call_different_single_post');
It's calling the single-1.php template for the single posts.
Now I want to check this template conditionally so that I can call some other js files like
function call_cust_js_single(){
if( /*..this is single-1.php template..*/){
wp_register_script('cust-js', get_stylesheet_directory_uri() . 'custom.js', false, '1.0.0');
wp_enqueue_script('cust-js');
}
}
add_action('wp_enqueue_scripts', 'call_cust_js_single');
There's a post similar to this but it doesn't quite cover your request. Here's the link here
Here's the valuable code:
add_filter( 'template_include', 'var_template_include', 1000 );
function var_template_include( $t ){
$GLOBALS['current_theme_template'] = basename($t);
return $t;
}
function get_current_template( $echo = false ) {
if( !isset( $GLOBALS['current_theme_template'] ) )
return false;
if( $echo )
echo $GLOBALS['current_theme_template'];
else
return $GLOBALS['current_theme_template'];
}
Now you have the function get_current_template that will return the filename of the template file.
Now edit your header.php file, here's an example:
<?php if('single-1.php' == get_current_template())
{
//load your scripts here
} ?>
It's not as clean as what you were looking for but I think it will help you.

Resources