WordPress Script Isn't Working

I learned how to code basic PHP years ago, but I got locked out when I started using WordPress. I finally decided to learn how to use PHP with WordPress, though I’m gettng off to a rocky start. I hired my webhost to create a child theme and write a script that displays an ID on each page’s body tag.

I then asked them to create another script(s) that I explain more fully at symbols.geobop.com/help

They told me that their WordPress support doesn’t include developing. However, they did write a script, which they told me to past into functions.php. Unfortunately, it doesn’t work. The web page I’m practicing on does display “I surrender,” which I added to the script as an else statement.

So, it isn’t clear to me if 1) there’s a bug in the second script they gave me, or 2) I simply combined the two scripts incorrectly. (I simply pasted the second script at the very end of the first script.) If you have time to glance at my script, can you see any obvious errors?

<?php
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;

// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED - Do not modify or remove comment markers above or below:

if ( !function_exists( 'chld_thm_cfg_locale_css' ) ):
    function chld_thm_cfg_locale_css( $uri ){
        if ( empty( $uri ) && is_rtl() && file_exists( get_template_directory() . '/rtl.css' ) )
            $uri = get_template_directory_uri() . '/rtl.css';
        return $uri;
    }
endif;
add_filter( 'locale_stylesheet_uri', 'chld_thm_cfg_locale_css' );
         
if ( !function_exists( 'child_theme_configurator_css' ) ):
    function child_theme_configurator_css() {
        wp_enqueue_style( 'chld_thm_cfg_child', trailingslashit( get_stylesheet_directory_uri() ) . 'style.css', array( 'astra-theme-css' ) );
    }
endif;

function my_custom_body_id() {
    // If we're on a page, use the page slug for the ID.
    if (is_page()) {
        global $post;
        return $post->post_name;
    }

    // If we're on a single post, use the post type and post name.
    if (is_single()) {
        global $post;
        return $post->post_name;
    }

    // If we're on the home page, use a specific ID.
    if (is_front_page()) {
        return 'home-page';
    }

    // Add more conditional tags for other types of pages as needed.
    // ...

    // Return a default ID if none of the above conditions are met.
    return 'default-page';
}


add_action( 'wp_enqueue_scripts', 'child_theme_configurator_css', 10 );

// END ENQUEUE PARENT ACTION



function bottom_home_shortcode($atts) {
    global $post;
    $ID = $post->ID;
    $Parent = '';
    $Parent_URL = '';
    $bottom_home = '';

    // Define parent pages
    switch ($ID) {
        case 'usa':
        case 'can':
        case 'mex':
            $Parent = 'North America';
            $Parent_URL = 'north-america';
            break;
        case 'al':
        case 'ak':
        case 'ny':
            $Parent = 'United States';
            $Parent_URL = 'usa';
            break;
    }

    // Special case for North American Flags
    if ($ID == 'us-flags') {
        $Parent = 'North American Flags';
        $Parent_URL = 'north-america-flags';
    }

    // Determine what to display based on the parent
    if ($Parent == 'North America' || $Parent == 'United States') {
        $bottom_home = 'ABC<div class="div–img div–home x–w25"><a href="/'.$Parent_URL.'/" title="'.$Parent.'"><img src="/wp-content/uploads/'.$Parent_URL.'-home.svg" alt="'.$Parent.' Home"></a></div>';
    } elseif ($ID == 'us-flags') {
        $bottom_home = 'XYZ<table class="table–85 x–w30">
            <tr>
                <td style="width: 50%;"><a href="/flag/" title="Flags"><img src="/wp-content/uploads/flag-home.svg" alt="Flags"></a></td>
                <td><a href="/usa/" title="United States"><img src="/wp-content/uploads/usa-home.svg" alt="United States"></a></td>
            </tr>
        </table>';
    }



else

{ $bottom_home = 'I surrender!'; }




    return $bottom_home;
}

add_shortcode('bottom-home', 'bottom_home_shortcode');

PS - I added this at the end of the script . . .

$bottom-home = $ID;

Now it displays something - but not the ID. It displays the numeral 294. If I replace $ID with $Parent or $Parent_URL, it displays nothing.

Post IDs in wordpress are always numbers, so that switch there that matches against usa, can, mex, etc, will never hit anything, as a number is never a string. Not sure what you expected there, but this won’t do anything.

1 Like

These aren’t posts; they’re pages - and they have alphanumeric ID’s. The ID of the page I’m practicing on is ‘usa’.

Oh, I think I know what you mean . . . In the code, they wrote global $post. Maybe that should be global $page, or something like that? On the other hand, that’s the part of the code that works.

As strange as it may sound, pages in WordPress are essentially a specific type of post with a post type value of “page”. Both are stored in the same database table and managed similarly in the backend.

Nope. global $post is correct.

That damn WordPress does everything it can to make things confusing. I used to be able to write PHP switches and modify them easily. However, the code they use in WordPress is so convoluted, I can’t figure out what half of it means.