1

I'm trying to generate a custom array from a PHP function to pass on wp_localize_script but I seem to not be able to set page conditionals.

I have the following function:

function mb_scripts_settings() {

    // blanks
    $mb_ajax_form_type = $mb_get_page_slug = $mb_redirect = $mb_redirect_time = $mb_form_disable = $mb_array = '';


    // get the form type
    $mb_ajax_form_type      = ( is_front_page() ? 'change'  : 'submit'  );

    // get the page
    $mb_get_page_slug       = get_page_link();

    // generate the url for redirection
    $mb_form_area           = ( ( is_page('admin') && isset($_GET['mbtab']) )   ? $_GET['mbtab']    : null  );
    $mb_form_area_url       = ( empty($mb_form_area)    ? '/' : '/admin/?mbtab=' . $mb_form_area . '&mbform=1'          );

    // if the page is admin
    if( is_page('admin') ) {
        $mb_redirect        = true;
        $mb_redirect_time   = 3000;
        $mb_form_disable    = true;
    }

    // if the page is password set
    if( is_page('pw') ) {
        $mb_redirect        = true;
        $mb_redirect_time   = 3000;
        $mb_form_disable    = true;
    }

    // if the page is front
    if( is_page('pw') ) {
        $mb_redirect        = false;
        $mb_redirect_time   = 0;
        $mb_form_disable    = false;
        $mb_form_area = $mb_form_area_url = '';
    }

    // build the array
    $mb_array = array( $mb_ajax_form_type, $mb_get_page_slug, $mb_redirect, $mb_redirect_time, $mb_form_disable );

    return $mb_array;
}

However, I am getting an error saying I can't call is_front_page or is_page outside of the loop.

Notice: is_front_page was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.0.)

I have tried to do this by getting the page id and then the slug from that but it hasn't returned any results - always blank in the mb_get_page_slug

function mb_scripts() {
    wp_enqueue_script( 'mbtheme_js', mbWebOS_js . 'theme-scripts. '.js' );
    wp_localize_script( 'mbtheme_js', 'mbtheme_js', mb_scripts_settings() );
}

add_action( 'wp_enqueue_scripts',   'mb_scripts'        );
10
  • In what hook do you call mb_scripts_settings() ? Is it wp_enqueue_scripts?
    – Sally CJ
    Commented Nov 21, 2019 at 4:54
  • Where is mb_scripts_settings() running? Where is wp_localize_script()? Commented Nov 21, 2019 at 4:55
  • @SallyCJ added it, sorry thought I had!
    – markb
    Commented Nov 21, 2019 at 5:05
  • Other than the typo in the code, your code seems fine to me. What's the exact error message? But you can try installing Query Monitor and visit the page in question, then inspect the Query Monitor results.
    – Sally CJ
    Commented Nov 21, 2019 at 5:46
  • 1
    @SallyCJ not sure what’s happened but i wrote it all again and now it works. potentially a rogue character or just missed a letter.
    – markb
    Commented Nov 21, 2019 at 7:05

1 Answer 1

0

Ended up with this as the function:

// set up the settings for the theme.js
function mb_scripts_settings() {

    // blanks
    $mb_ajax_form_type = $mb_get_page_slug = $mb_redirect = $mb_redirect_time = $mb_form_disable = $mb_array = '';

    // get the form type
    $mb_ajax_form_type      = ( is_front_page() ? 'change'  : 'submit'  );

    // get the page slug from ID
    $mb_get_page_slug       = get_post_field( 'post_name', get_the_ID() );

    // if the page is admin or password
    if( is_page( array('admin', 'pw') ) ) {
        $mb_redirect        = true;
        $mb_redirect_time   = 3000;
        $mb_form_disable    = true;

        if( is_page('admin') ) {
            // generate the url for redirection
            $mb_form_area           = ( ( is_page('admin') && isset($_GET['mbtab']) )   ? $_GET['mbtab']    : null      );
            $mb_form_area_url       = ( empty($mb_form_area)    ? '/' : '/admin/?mbtab=' . $mb_form_area . '&mbform=1'  );
            $mb_form_area_url       = get_home_url( $mb_form_area_url );
        }
    }

    // if the page is front
    if( is_front_page() ) {
        $mb_redirect        = false;
        $mb_redirect_time   = 0;
        $mb_form_disable    = false;
        $mb_get_page_slug   = 'front_page';
        $mb_form_area = $mb_form_area_url = null;
    }

    // build the array
    $mb_array = array(
                $mb_ajax_form_type,
                $mb_get_page_slug,
                $mb_redirect,
                $mb_redirect_time,
                $mb_form_disable
            );

    return $mb_array;
}

And when enqueuing the script:

    // enqueue the theme js
    wp_enqueue_script( 'mb_mbtheme_js', mbWebOS_js . 'scripts.' . $mbTheme . '.js' );

    // localise the theme js
    // only for selected pages
    if( is_page('admin') || is_front_page() ) {
        wp_localize_script( 'mb_mbtheme_js', 'mb_mbtheme_js', mb_scripts_settings() );
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.