admin管理员组

文章数量:1405170

I'm working on a fill-in-the-blanks plugin () which has been working.

I realized that the plugin was loading its styles, js, and inserting a div on every page, not just its own custom post type ('lqdnotes). So I used a simple if statement to ensure the code is only passed if the cpt is present.

It occurs in the following locations:

lqd-notes.php

function lqdnotes_enqueue_css() {
    if ( get_post_type( get_the_ID() ) == 'lqdnotes' ) {
        $lqdcssversion = filemtime( LQDNOTES_DIR . 'public/css/lqdnotes.css' );
        wp_enqueue_style(
            'lqdnotes-css',
            plugins_url(  'public/css/lqdnotes.css', __FILE__ ),
            array(),
            $lqdcssversion
        );
    }
}

add_action( 'enqueue_block_assets', 'lqdnotes_enqueue_css' );

public/display-filled.php

function lqdnotes_enqueue_display_filled() {
    if ( get_post_type( get_the_ID() ) == 'lqdnotes' ) {
        $lqdfilterinputversion = filemtime( LQDNOTES_DIR . 'public/js/lqdnotes-filter-inputs.js' );
        wp_enqueue_script(
            'lqdnotes-filter-inputs',
            LQDNOTES_URL . 'public/js/lqdnotes-filter-inputs.js',
            array( 'jquery' ),
            $lqdfilterinputversion
        );

        $ajax_array = array(
            'ajax_url' => admin_url( 'admin-ajax.php' )
        );

        wp_localize_script(
            'lqdnotes-filter-inputs',
            'lqdnotes_ajax',
            $ajax_array );
    }
}
add_action( 'wp_enqueue_scripts', 'lqdnotes_enqueue_display_filled' );

public/modify-display.php

function lqdnotes_enqueue_display_blanks() {
    if ( get_post_type( get_the_ID() ) == 'lqdnotes' ) {
        $lqdfilterspanversion = filemtime( LQDNOTES_DIR .'public/js/lqdnotes-filter-span.js' );
        wp_enqueue_script(
            'lqdnotes-filter-spans',
            plugin_dir_url( __FILE__ ) . 'js/lqdnotes-filter-span.js',
            array(),
            $lqdfilterspanversion
        );
    }
}
add_action( 'wp_enqueue_scripts', 'lqdnotes_enqueue_display_blanks' );
function lqdnotes_add_div( $content ) {
    if ( get_post_type( get_the_ID() ) == 'lqdnotes' ) {
        $updated_content = '<div id="message-notes" class="message-notes">' . $content . '</div>';
        return $updated_content;
    }
}
add_filter( 'the_content', 'lqdnotes_add_div' );

When I activate the plugin and browser to a page of content (say test.liquidchurch) the header and footer display but no page content..and this is occurring on pages that aren't part of the CPT.

If, however, I open one of the CPT pages everything displays correctly (see /).

To see what a page on test.liquidchurch should look like go to liquidchurch. As one can see, there is content for the page and if I were to disable the notes plugin on test.liquidchurch it would appear there as well (I've disabled the plugin on the live site until this is resolved).

I'm working on a fill-in-the-blanks plugin (https://github/liquidchurch/lqd-notes) which has been working.

I realized that the plugin was loading its styles, js, and inserting a div on every page, not just its own custom post type ('lqdnotes). So I used a simple if statement to ensure the code is only passed if the cpt is present.

It occurs in the following locations:

lqd-notes.php

function lqdnotes_enqueue_css() {
    if ( get_post_type( get_the_ID() ) == 'lqdnotes' ) {
        $lqdcssversion = filemtime( LQDNOTES_DIR . 'public/css/lqdnotes.css' );
        wp_enqueue_style(
            'lqdnotes-css',
            plugins_url(  'public/css/lqdnotes.css', __FILE__ ),
            array(),
            $lqdcssversion
        );
    }
}

add_action( 'enqueue_block_assets', 'lqdnotes_enqueue_css' );

public/display-filled.php

function lqdnotes_enqueue_display_filled() {
    if ( get_post_type( get_the_ID() ) == 'lqdnotes' ) {
        $lqdfilterinputversion = filemtime( LQDNOTES_DIR . 'public/js/lqdnotes-filter-inputs.js' );
        wp_enqueue_script(
            'lqdnotes-filter-inputs',
            LQDNOTES_URL . 'public/js/lqdnotes-filter-inputs.js',
            array( 'jquery' ),
            $lqdfilterinputversion
        );

        $ajax_array = array(
            'ajax_url' => admin_url( 'admin-ajax.php' )
        );

        wp_localize_script(
            'lqdnotes-filter-inputs',
            'lqdnotes_ajax',
            $ajax_array );
    }
}
add_action( 'wp_enqueue_scripts', 'lqdnotes_enqueue_display_filled' );

public/modify-display.php

function lqdnotes_enqueue_display_blanks() {
    if ( get_post_type( get_the_ID() ) == 'lqdnotes' ) {
        $lqdfilterspanversion = filemtime( LQDNOTES_DIR .'public/js/lqdnotes-filter-span.js' );
        wp_enqueue_script(
            'lqdnotes-filter-spans',
            plugin_dir_url( __FILE__ ) . 'js/lqdnotes-filter-span.js',
            array(),
            $lqdfilterspanversion
        );
    }
}
add_action( 'wp_enqueue_scripts', 'lqdnotes_enqueue_display_blanks' );
function lqdnotes_add_div( $content ) {
    if ( get_post_type( get_the_ID() ) == 'lqdnotes' ) {
        $updated_content = '<div id="message-notes" class="message-notes">' . $content . '</div>';
        return $updated_content;
    }
}
add_filter( 'the_content', 'lqdnotes_add_div' );

When I activate the plugin and browser to a page of content (say test.liquidchurch) the header and footer display but no page content..and this is occurring on pages that aren't part of the CPT.

If, however, I open one of the CPT pages everything displays correctly (see https://test.liquidchurch/blog/notes/breakthrough/).

To see what a page on test.liquidchurch should look like go to liquidchurch. As one can see, there is content for the page and if I were to disable the notes plugin on test.liquidchurch it would appear there as well (I've disabled the plugin on the live site until this is resolved).

Share Improve this question asked Dec 20, 2019 at 17:25 davemackeydavemackey 3152 silver badges18 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

I realize you've already answered your issue. However, I think there is some additional explanation possible to give more clarity to the issue and the solution.

The issue is your lqdnotes_add_div() function. This is hooked to a filter - the_content.

In WordPress, any time you use a filter, your filter function must return a value for the item being filtered. This might be a string, array, or boolean. In this case, $content is a string, and a string must be returned.

Your filter function only returned a value if the post type was "lqdnotes". In all other cases a null value was returned.

While you do need to return a value for $content, you do not need to do this with an else condition. In fact, it is a good standard practice to make sure there is at least a return value at the end of any filter function to provide for returning an unfiltered result if any of the function's other conditions are not met.

function lqdnotes_add_div( $content ) {
    if ( get_post_type( get_the_ID() ) == 'lqdnotes' ) {
        $updated_content = '<div id="message-notes" class="message-notes">' . $content . '</div>';
        return $updated_content;
    }
    return $content;
}
add_filter( 'the_content', 'lqdnotes_add_div' );

StackOverflow may be my rubber duck. Almost immediately after publishing the question the solution popped into my mind.

The problem was in the function lqdnotes_add_div. This function took $content (the post content) as a parameter. If post was not of type lqdnotes then it didn't perform any operation on $content...or so I thought.

PHP actually returns null if a return value is not specified, so actually was performing an action on $content - setting it to null.

Add an else clause that explicitly returns the $content I received fixes the issue:

function lqdnotes_add_div( $content ) {
    if ( get_post_type( get_the_ID() ) == 'lqdnotes' ) {
        $updated_content = '<div id="message-notes" class="message-notes">' . $content . '</div>';
        return $updated_content;
    } else {
        return $content;
    }
}

本文标签: