• Hello WP forum, I hope someone here can help me.

    I am using WordPress 4.4.1 and a twenty fifteen child theme.
    In fuctions.php I have added the following code so that every time there is an asterisk (*) it is automatically surrounded by span tags sop that I can center align it in the css targeting the .auto-style class.

    add_filter( 'the_content', 'auto_word_style' );
    function auto_word_style( $text ) {
    $text = str_replace( '*', '<span class="auto-style">* * *</span>', $text );
    return $text;

    But I need to run the code above only for a specific page template (my-template.php), so I changed the code into the following, but it doesn’t work.

    if ( page_template ( "my-template.php") ) {
    	add_filter( 'the_content', 'auto_word_style' );
    	function auto_word_style( $text ) {
    	$text = str_replace( '*', '<span class="auto-style">* * *</span>', $text );
    	return $text;
    	}
    }

    Thanks in advance,
    Beppe

Viewing 11 replies - 1 through 11 (of 11 total)
  • I believe you are looking for the function is_page_template() not page_template() – Looks like it should work fine once you call the right function.

    https://developer.www.ads-software.com/reference/functions/is_page_template/

    Thread Starter gippo

    (@gippo)

    Thank you Craig for your reply and for spotting my mistake, but it was only a copy/paste mistake, in fact I did use is_page_template ( ) but it doesn’t work.
    My investigation lead me to think that the above mentioned function doesn’t work with the loop and it should be used instead the function get_page_template_slug ( ) so I tried to rewrite the code, posted below (please note: “page-templates” is the directory where the page templates are stored), but even this one doesn’t work.

    if (get_page_template_slug ( $post->id ) == "page-templates/my-template.php") {
    	add_filter( 'the_content', 'auto_word_style' );
    	function auto_word_style( $text ) {
    	$text = str_replace( '*', '<span class="auto-style">* * *</span>', $text );
    	return $text;
    	}
    }

    Hey @gippo,

    So it looks like you are trying to use the global post object before it is set. I imagine you have a bunch of PHP notices in your logs to the effect of “trying to get property of non-object” from your code above.

    Try breaking this out into a function + helper function and hook it somewhere after the post object is set, like the_post.

    function checkPageTemplate(){
    
    	// grab post object
    	$currentPost = get_post();
    
    	// grab post template value
    	$pageTemplate = get_post_meta( $currentPost->ID, '_wp_page_template', true );
    
    	// compare them
    	if ( $pageTemplate == "page-templates/my-template.php") {
    
    		// we have a match, so let's filter the content now
    		add_filter( 'the_content', 'auto_word_style' );
    	}
    }
    add_action( 'the_post', 'checkPageTemplate' );
    
    function auto_word_style( $text ) {
    	$text = str_replace( '*', '<span class="auto-style">* * *</span>', $text );
    	return $text;
    }
    Thread Starter gippo

    (@gippo)

    Well, it works!
    Thank you for your help and for commenting your code, quite instructive.

    I don’t understand completely yet what you are saying about my PHP notices, but it’s a starting point for me to learn a little bit more.

    Glad to hear it’s working for you.

    What I meant about the PHP notices, was that if you view your server error logs, there are likely clues to what was going wrong. There were likely “undefined variable on line X” and “trying to get property of non-object on line Y” messages which can usually help a ton in the troubleshooting process.

    Best of luck to you!

    hi, thanks for the code.

    But what about if we want to execute it for the single product page woocommerce template?

    Thanks
    angelo

    Moderator bcworkz

    (@bcworkz)

    Hi Angelo,
    The suggested code only works for pages. Single products all use the same template. If you want to know if a query is for a single product or not you can use is_single() or is_singular().

    If you have further questions, or for future reference, please start your own topic instead of tagging on to other’s topics. It’s the preferred practice in these forums.

    I’m using the code that Craig posted above but tried to modify it for the template and filter I’m using, but it’s not working for me, see the code below.

    (the function will take the woocommerce user straight to the checkout page after clicking “add to cart” , this should only work on the landing-page.php template)

    function checkPageTemplate(){
    
    $currentPost = get_post();
    $pageTemplate = get_post_meta( $currentPost->ID, '_wp_page_template', true );
    
    if ( $pageTemplate == "landing-page.php") {
    add_filter ('woocommerce_add_to_cart_redirect', 'redirect_to_checkout');
    }
    }
    add_action( 'the_post', 'checkPageTemplate' );
    
    function redirect_to_checkout() {
    global $woocommerce;
    $checkout_url = $woocommerce->cart->get_checkout_url();
    return $checkout_url;
    }

    Is there something wrong with this code?

    Moderator bcworkz

    (@bcworkz)

    Hi Clinton,
    The code appears OK, but Woo templates go through many layers before the add to cart button is output, more chances of something going awry. Instead of hooking ‘the_post’, try adding the redirect filter right on landing-page.php as the first line of code.

    If you still have trouble, then there is something about Woo templates preventing this from working. You may need to use a different approach.

    I know your issue is related to the OP’s, but we would still rather you start a new topic instead of tagging onto someone else’s. It’s not a big deal either way, but everyone starting their own topic keeps the forum’s statistics more accurate. Additionally, more people will see your new topic because most experienced members here only look for posts using the “No Replies” filter – the people in the best position to help you never see your post in this thread. Your cooperation would be appreciated.

    Hi there bcworkz,

    Thanks for the reply! I appreciate your help.

    Yes, I opened up a brand new thread about this problem a few days before I posted here:
    https://www.ads-software.com/support/topic/apply-function-to-specific-page-template?replies=2#post-8449648

    I wasn’t getting anywhere there so I wanted to try my luck here, then update that thread once solved.

    I think your solution sounds like a much better way of doing it and is an alternative solution to the subject of this thread.

    When I put the code at the top of the template, I’m getting errors. I’m not so great with php.

    Can you show me how to add this code

    <?php add_filter ('woocommerce_add_to_cart_redirect', 'redirect_to_checkout');
    function redirect_to_checkout() {
    global $woocommerce;
    $checkout_url = $woocommerce->cart->get_checkout_url();
    return $checkout_url;
    }
    ?>

    to this template?

    <?php /* Template Name: Landing Page */ ?>
    
    <?php
    get_header ( "landing" );
    ?>
    
    	<div id="primary" class="content-area">
    		<main id="main" class="site-main" role="main">
    
    			<?php while ( have_posts() ) : the_post();
    
    				do_action( 'storefront_page_before' );
    
    				get_template_part( 'content', 'page' );
    
    				/**
    				 * Functions hooked in to storefront_page_after action
    				 *
    				 * @hooked storefront_display_comments - 10
    				 */
    				do_action( 'storefront_page_after' );
    
    			endwhile; // End of the loop. ?>
    
    		</main><!-- #main -->
    	</div><!-- #primary -->
    
    <?php
    get_footer();
    Moderator bcworkz

    (@bcworkz)

    Clinton,
    Thank you for explaining about the making your own topic rule. I unfortunately missed your initial post. In that case it’s just as well that you tagged on to this thread because you’re not supposed to make duplicate posts in different forums either ?? Another reason for not tagging onto other’s topics is it diverts attention from the OP’s issue. Seeing that the OP’s issue was already resolved, no harm no foul ??

    Anyway, in an effort to keep related posts together, I’ll continue our discussion in your original thread.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Apply add:filter to a specific page template’ is closed to new replies.