wordpressfunctiontemplatescustom-wordpress-pages

How to access custom functions defined in functions.php from a single post template file


I have a Wordpress site that I'm trying to do some customization on. I have a post type called "Advocate". I am using the child theme "Blocksy-child". I open up the Blocksy-child theme and in that theme is a functions.php file. I open up functions.php and I add a new function to that and try to call the function in my single-advocate.php page. I get "Call to undefined function blocksy_post_advocate_navigation()"

Here is part of my functions.php found in the child theme:

/**
 * Output post navigation.
 */
if (! function_exists('blocksy_post_advocate_navigation')) {
function blocksy_post_advocate_navigation() {
    //echo "<br /><br /> begin blocksy_post_advocate_navigation <br /><br />";
    $prefix = blocksy_manager()->screen->get_prefix();

    $next_post = apply_filters(
        'blocksy:post-navigation:next-post',
        get_adjacent_post_bulletin_or_advocate(false, '', true)
    );
    //var_dump($next_post);

    $previous_post = apply_filters(
        'blocksy:post-navigation:previous-post',
        get_adjacent_post_bulletin_or_advocate(false, '', false)
    );

    $post_nav_criteria = blocksy_get_theme_mod($prefix . '_post_nav_criteria', 'default');

    if ($post_nav_criteria !== 'default') {
        $post_type = get_post_type();
        $post_nav_taxonomy_default = array_keys(blocksy_get_taxonomies_for_cpt(
            $post_type
        ))[0];

        $post_nav_taxonomy = blocksy_get_theme_mod(
            $prefix . '_post_nav_taxonomy',
            $post_nav_taxonomy_default
        );

        $next_post = apply_filters(
            'blocksy:post-navigation:next-post',
            get_adjacent_post(true, '', true, $post_nav_taxonomy)
        );

        $previous_post = apply_filters(
            'blocksy:post-navigation:previous-post',
            get_adjacent_post(true, '', false, $post_nav_taxonomy)
        );
    }

    if (! $next_post && ! $previous_post) {
        return '';
    }

    $title_class = 'item-title';

    $title_class .= ' ' . blocksy_visibility_classes(blocksy_get_theme_mod(
        $prefix . '_post_nav_title_visibility',
        [
            'desktop' => true,
            'tablet' => true,
            'mobile' => false,
        ]
    ));

    $thumb_size = blocksy_get_theme_mod($prefix . '_post_nav_thumb_size', 'medium');

    $thumb_class = '';

    $thumb_class .= ' ' . blocksy_visibility_classes(blocksy_get_theme_mod(
        $prefix . '_post_nav_thumb_visibility',
        [
            'desktop' => true,
            'tablet' => true,
            'mobile' => true,
        ]
    ));

    $container_class = 'post-navigation ct-constrained-width';

    $container_class .= ' ' . blocksy_visibility_classes(blocksy_get_theme_mod(
        $prefix . '_post_nav_visibility',
        [
            'desktop' => true,
            'tablet' => true,
            'mobile' => true,
        ]
    ));

    $home_page_url = get_home_url();

    $post_slug = get_post_type_object(get_post_type())->labels->singular_name;
    $post_slug = '<span>' . $post_slug . '</span>';

    $next_post_image_output = '';
    $previous_post_image_output = '';

    if ($next_post) {
        $next_title = '';

        $next_title = $next_post->post_title;

        if (get_post_thumbnail_id($next_post)) {
            $next_post_image_output = blocksy_media(
                [
                    'attachment_id' => get_post_thumbnail_id($next_post),
                    'post_id' => $next_post->ID,
                    'ratio' => '1/1',
                    'size' => $thumb_size,
                    'class' => $thumb_class,
                    'inner_content' => '<svg width="20px" height="15px" viewBox="0 0 20 15" fill="#ffffff"><polygon points="0,7.5 5.5,13 6.4,12.1 2.4,8.1 20,8.1 20,6.9 2.4,6.9 6.4,2.9 5.5,2 "/></svg>',
                    'tag_name' => 'figure'
                ]
            );
        }
    }

    if ($previous_post) {
        $previous_title = '';

        $previous_title = $previous_post->post_title;

        if (get_post_thumbnail_id($previous_post)) {
            $previous_post_image_output = blocksy_media(
                [
                    'attachment_id' => get_post_thumbnail_id($previous_post),
                    'post_id' => $previous_post->ID,
                    'ratio' => '1/1',
                    'size' => $thumb_size,
                    'class' => $thumb_class,
                    'inner_content' => '<svg width="20px" height="15px" viewBox="0 0 20 15" fill="#ffffff"><polygon points="14.5,2 13.6,2.9 17.6,6.9 0,6.9 0,8.1 17.6,8.1 13.6,12.1 14.5,13 20,7.5 "/></svg>',
                    'tag_name' => 'figure'
                ]
            );
        }
    }

    $prefix = blocksy_manager()->screen->get_prefix();

    $deep_link_args = [
        'prefix' => $prefix,
        'suffix' => $prefix . '_has_post_nav'
    ];



    ob_start();


    ?>


        <nav class="<?php echo esc_attr( $container_class ); ?>" <?php echo blocksy_generic_get_deep_link($deep_link_args); ?>>
            <?php if ($next_post) { ?>
                <a href="<?php echo esc_url(get_permalink($next_post)); ?>" class="nav-item-prev">
                    <?php
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                        echo $next_post_image_output;
                    ?>

                    <div class="item-content">
                        <span class="item-label">
                            <?php
                                echo wp_kses_post(blocksy_safe_sprintf(
                                    apply_filters(
                                        'blocksy:post-navigation:previous-post:label',
                                        // translators: post title
                                        //__('Previous %s', 'blocksy')
                                        __('Previous Post', 'blocksy')
                                    ),
                                    $post_slug
                                ));
                            ?>
                        </span>

                        <?php if ( ! empty( $next_title ) ) { ?>
                            <span class="<?php echo esc_attr( $title_class ); ?>">
                                <?php echo wp_kses_post($next_title); ?>
                            </span>
                        <?php } ?>
                    </div>

                </a>
            <?php } else { ?>
                <div class="nav-item-prev"></div>
            <?php } ?>

            <?php if ($previous_post) { ?>
                <a href="<?php echo esc_url(get_permalink($previous_post)); ?>" class="nav-item-next">
                    <div class="item-content">
                        <span class="item-label">
                            <?php
                                echo wp_kses_post(blocksy_safe_sprintf(
                                    apply_filters(
                                        'blocksy:post-navigation:next-post:label',
                                        // translators: post title
                                        //__('Next %s', 'blocksy')
                                        __('Next Post', 'blocksy')
                                    ),
                                    $post_slug
                                ));
                            ?>
                        </span>

                        <?php if ( ! empty( $previous_title ) ) { ?>
                            <span class="<?php echo esc_attr( $title_class ); ?>">
                                <?php echo wp_kses_post($previous_title); ?>
                            </span>
                        <?php } ?>
                    </div>

                    <?php
                        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                        echo $previous_post_image_output;
                    ?>
                </a>
            <?php } else { ?>
                <div class="nav-item-next"></div>
            <?php } ?>

        </nav>

    <?php

    return ob_get_clean();
}
}

and here is my single-advocate.php template file:

<?php
/**
 * The template for displaying all single advocate posts
 */



get_header();

$fieldValues = get_fields();

$url = $fieldValues["pdf"]["url"];

?>
<div id="advocate-pdf-container" class="visibility-hidden">
    <?php echo $url ?>
</div>
<div class='book_container'>
    <div id="book"></div>
</div>

<?php
echo blocksy_post_advocate_navigation();



get_footer();

and here is the exact error:

2024/10/29 20:53:53 [error] 36568#36568: *22207 FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught Error: Call to undefined function blocksy_post_advocate_navigation() in /www/elawenvironmentallawalliance_573/public/wp-content/themes/blocksy-child/single-advocate.php:23
Stack trace:
#0 /www/elawenvironmentallawalliance_573/public/wp-includes/template-loader.php(106): include()
#1 /www/elawenvironmentallawalliance_573/public/wp-blog-header.php(19): require_once('/www/elawenviro...')
#2 /www/elawenvironmentallawalliance_573/public/index.php(17): require('/www/elawenviro...')
#3 {main}
thrown in /www/elawenvironmentallawalliance_573/public/wp-content/themes/blocksy-child/single-advocate.php on line 23" while reading response header from upstream, client: 69.9.143.165, server: env-elawenvironmentallawalliance-premium.kinsta.cloud, request: "GET /advocate/autumn-advocate-2023 HTTP/2.0", upstream: "fastcgi://unix:/var/run/php8.1-fpm-elawenvironmentallawalliance.sock:", host: "env-elawenvironmentallawalliance-premium.kinsta.cloud:34967", referrer: "https://env-elawenvironmentallawalliance-premium.kinsta.cloud/wp-admin/post.php?post=53428&action=edit"

I even tried making a really simple function in functions.php called my_test_function() that just returned the post type and tried executing that from single-advocate.php with the same call to undefined function.

I thought I could just write a function here and all it from any Wordpress page.

functions.php and single-advocate.php are both located in the same child theme directory.

UPDATE As mentioned above I use a child theme "Blocksy-Child" which is a child of Blocksy. I can add a custom function to "Blocksy" functions.php (not the child theme) and use it in single-advocate.php.

enter image description here

No functions in the functions.php file can be executed from single-advocate.php.

Can anyone help me figure this out?


Solution

  • I finally figured it out. Our functions.php (which was created by a contractor for us) has a namespace defined at the top of the file:

    <?php
    /**
     * Blocksy-child functions and definitions.
     *
     * @link https://developer.wordpress.org/themes/basics/theme-functions/
     *
     * @package Crate
     */
    
    namespace CShop\Blocksy;
    ...
    

    Apparently this was what was causing my functions to not work. I updated my single-advocate.php to have the same namespace at the top of the file and my functions are now accessible.

    <?php
    /**
     * The template for displaying all single advocate posts
     */
    
    namespace CShop\Blocksy;
    
    get_header();
    
    $fieldValues = get_fields();
    
    $url = $fieldValues["pdf"]["url"];
    
    ?>
    <div id="advocate-pdf-container" class="visibility-hidden">
        <?php echo $url ?>
    </div>
    <div class='book_container'>
        <div id="book"></div>
    </div>
    
    <?php
    echo blocksy_post_advocate_navigation();
    
    get_footer();