WordPress.org

Ready to get started?Download WordPress

Forums

Function not attaching to wp_footer hook (15 posts)

  1. Eric Hoanshelt
    Member
    Posted 1 year ago #

    I'm having some issues getting this functionality to work. It must be something I'm overlooking. This is my code:

    if ( is_page_template('template-thankyou.php') ) {
    
        function eh_add_product_tracking_code(){
            $meta_key = 'product_code';
            $meta_value = get_post_meta($post->ID, $meta_key, true);
    
            return $meta_value;
        }
    
        add_action('wp_footer', 'eh_add_product_tracking_code');
    
    }

    Basically, I'm trying to add a custom field named product_code to the footer of the template-thankyou.php page from the functions.php.

    As you can see, I check to see if we are on a page using the correct template. Then I use a function to get the custom field text and return it. Then I add it to the footer.

    The footer shows nothing. Any ideas?

  2. Pioneer Valley Web Design
    Member
    Posted 1 year ago #

    Does the template in use include:

    <?php get_footer(); ?>

    ??

    Typically, it's the last line.

  3. Eric Hoanshelt
    Member
    Posted 1 year ago #

    Hey :)
    Yes it does

  4. birgire
    Member
    Posted 1 year ago #

    It looks like you are missing the

    <?php wp_footer();?>

    in footer.php

  5. Eric Hoanshelt
    Member
    Posted 1 year ago #

    Hey Birgire,

    No, I have the wp_footer(); in the footer.php file. My other function are working fine that are attached to it.

  6. birgire
    Member
    Posted 1 year ago #

    What happens if you skip the first if-sentence ?

    Just to see if it runs then?

  7. Eric Hoanshelt
    Member
    Posted 1 year ago #

    Here is the page i'm working on

    http://www.learnwebdevelopment.com/crazy

  8. birgire
    Member
    Posted 1 year ago #

    or mabye you need

    global $post;

    insde the function to get access to the value of $post->ID

  9. Eric Hoanshelt
    Member
    Posted 1 year ago #

    I tried that also but to no avail.

    This was the code I tried:

    //if ( is_page_template('template-thankyou.php') ) {
    
    		function eh_add_product_tracking_code(){
    			global $post;
    
    			$meta_key = 'product_code';
    			$meta_value = get_post_meta($post->ID, $meta_key, true);
    
    			if ($meta_value = ''){
    				$meta_value = "This is Blank!";
    			}else{
    				return $meta_value;
    			}
    		}
    
    		add_action('wp_footer', 'eh_add_product_tracking_code');
    
    //}
  10. birgire
    Member
    Posted 1 year ago #

    for the debug you could try

    if (strlen($meta_value)<1){
        $meta_value = "This is Blank!";
    }
    return $meta_value;

    instead of

    if ($meta_value = ''){
        $meta_value = "This is Blank!";
    }else{
        return $meta_value;
    }
  11. Eric Hoanshelt
    Member
    Posted 1 year ago #

    I added that and no dice.

    This is the template page:
    http://pastebin.com/jtURRup5

    This is the footer.php:
    http://pastebin.com/f1NJXPnk

    And the function:
    http://pastebin.com/7TsG9388

    I think this will help more

  12. birgire
    Member
    Posted 1 year ago #

    aha, think I found it:

    use

    echo $meta_value;

    instead of

    return $meta_value;

    ;-)

  13. Eric Hoanshelt
    Member
    Posted 1 year ago #

    Tried that also. I'm just going to go another approach at this and do it via a separate footer :P

  14. birgire
    Member
    Posted 1 year ago #

    damn ;-)

    strange, I can't see this comment

    <!-- /#wrapper -->

    in the html source for

    http://www.learnwebdevelopment.com/crazy/:

    Looks like we have to go back to the basics and try:

    <?php
    function mydebug(){
       echo "debug";
    }
    add_action('wp_footer', 'mydebug');
    ?>

    or can you maybe use the action on

    add_action( 'woo_foot' , 'mydebug');

    instead of

    add_action( 'wp_footer' , 'mydebug');

    ?

  15. birgire
    Member
    Posted 1 year ago #

    it looks like the definition of woo_foot() is

    function woo_foot() { do_action( 'woo_foot' ); }

    so the action hook woo_foot should work in theory ;-)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.