Getbeans / Beans

Beans WordPress Theme Framework. The default branch is set to development, please switch to the master branch for production.
https://www.getbeans.io
Other
392 stars 61 forks source link

REST API endpoints? #319

Closed paaljoachim closed 6 years ago

paaljoachim commented 6 years ago

I made this issue at the Gutenberg Github: https://github.com/WordPress/gutenberg/issues/8644

I have changed: "You can also change this check if ( is_singular() || is_admin() ) { return $content; } to check if your in a REST endpoint as well and return the content as is in that case."

I have also adjusted the if statement to become if ( !is_archive() ) { That only affects the custom post archive and not the blog page.

I apologize for perhaps making an issue which is in regards to code not working as it should. For this might not have anything to do with Beans core. I am trying to get this to work correctly so that I have the correct code on 2 tutorials that I am sharing at wpbeansframework.com. (So please forgive me for the spinning energy of sitting in code that halfway works.)

paaljoachim commented 6 years ago

I am testing the following excerpt code at wpbeansframework.com.

// https://community.getbeans.io/discussion/how-to-show-post-excerpts/#post-4764
add_filter( 'the_content', 'beans_child_modify_post_content' );
function beans_child_modify_post_content( $content ) {
// Stop here if we are on a single view.
if ( is_singular() || is_admin() ) {
return $content;
}

// Return the excerpt() if it exists other truncate.
if ( has_excerpt() )
$content = '<p>' . get_the_excerpt() . '</p>';
else
$content = '<p>' . wp_trim_words( get_the_content(), 40, '...' ) . '</p>';

// Return content and readmore.
return $content . '<p>' . beans_post_more_link() . '</p>';
}

When Gutenberg is activated the posts and pages will not auto save and can not be saved. See the above Gutenberg issue for the error mentioned.

if ( is_singular() || is_admin() ) { Causes an error as listed in the above Gutenberg issue.

if ( is_singular() ) { Works when Gutenberg is not active. When Gutenberg is active. Gives an error message when one clicks the edit post link in the top frontend admin bar.

) Fatal error: Uncaught Error: Call to undefined function beans_post_more_link() in /Users/joachim/Documents/Websites/beans-tests.dev.cc/wp-content/themes/tm-beans-blank-child/functions.php on line 49 ( ! ) Error: Call to undefined function beans_post_more_link() in /Users/joachim/Documents/Websites/beans-tests.dev.cc/wp-content/themes/tm-beans-blank-child/functions.php on line 49 Call Stack

Time Memory Function Location

1 0.0047 587176 {main}( ) .../post.php:0 2 0.3047 26608584 apply_filters( ) .../post.php:155 3 0.3047 26608984 WP_Hook->apply_filters( ) .../plugin.php:203 4 0.3047 26610488 gutenberg_init( ) .../class-wp-hook.php:286 5 0.3053 26639744 require_once( '/Users/joachim/Documents/Websites/beans-tests.dev.cc/wp-admin/admin-header.php' ) .../gutenberg.php:197 6 0.3072 26647888 do_action( ) .../admin-header.php:97 7 0.3072 26648264 WP_Hook->do_action( ) .../plugin.php:453 8 0.3072 26648264 WP_Hook->apply_filters( ) .../class-wp-hook.php:310 9 0.3192 26693248 gutenberg_editor_scripts_and_styles( ) .../class-wp-hook.php:286 10 0.3671 27449944 array_reduce ( ) .../client-assets.php:1189 11 0.3851 27837152 gutenberg_preload_api_request( ) .../client-assets.php:1189 12 0.3851 27838712 rest_do_request( ) .../client-assets.php:676 13 0.3851 27838712 WP_REST_Server->dispatch( ) .../rest-api.php:390 14 0.3866 27929200 WP_REST_Posts_Controller->get_item( ) .../class-wp-rest-server.php:936 15 0.3867 27929648 WP_REST_Posts_Controller->prepare_item_for_response( ) .../class-wp-rest-posts-controller.php:465 16 0.3881 27945696 apply_filters( ) .../class-wp-rest-posts-controller.php:1498 17 0.3881 27946096 WP_Hook->apply_filters( ) .../plugin.php:203 18 0.3884 27947976 beans_child_modify_post_content( ) .../class-wp-hook.php:286

Code on specific lines:

Line 48 // Return content and readmore.
Line 49 return $content . '<p>' . beans_post_more_link() . '</p>';
Line 50 }

!is_archive() Works with and without Gutenberg to add a continue reading to an archive page. Such as a custom post.

!is_home() Works with and without Gutenberg to add a continue reading to a blog page.

if ( !is_home() || !is_archive() ) Does not work. Even though cache has been refreshed... (With our without Gutenberg active.)

Using this code suggested by Joseph: https://community.getbeans.io/discussion/how-to-show-post-excerpts/#post-4764

add_filter( 'the_content', 'beans_child_modify_post_content' );
function beans_child_modify_post_content( $content ) {
    // Returns full content a single view.
    if ( is_singular() ) {
        return $content;
    }
    // Returns the custom excerpt or truncated content with read more link.
    return sprintf(
        '<p>%s</p><p>%s</p>',
        has_excerpt() ? get_the_excerpt() : wp_trim_words( $content, 40, '...' ),
        beans_post_more_link()
    );
}

Works for the blog and archive page when Gutenberg is not active. When Gutenberg is active. Gives the following error message when one clicks the edit post link in the top frontend admin bar.

) Fatal error: Uncaught Error: Call to undefined function beans_post_more_link() in /Users/joachim/Documents/Websites/beans-tests.dev.cc/wp-content/themes/tm-beans-blank-child/functions.php on line 45 ( ! ) Error: Call to undefined function beans_post_more_link() in /Users/joachim/Documents/Websites/beans-tests.dev.cc/wp-content/themes/tm-beans-blank-child/functions.php on line 45 Call Stack

Time Memory Function Location

1 0.0047 587176 {main}( ) .../post.php:0 2 0.3045 26608296 apply_filters( ) .../post.php:155 3 0.3045 26608696 WP_Hook->apply_filters( ) .../plugin.php:203 4 0.3045 26610200 gutenberg_init( ) .../class-wp-hook.php:286 5 0.3050 26639456 require_once( '/Users/joachim/Documents/Websites/beans-tests.dev.cc/wp-admin/admin-header.php' ) .../gutenberg.php:197 6 0.3064 26647600 do_action( ) .../admin-header.php:97 7 0.3064 26647976 WP_Hook->do_action( ) .../plugin.php:453 8 0.3064 26647976 WP_Hook->apply_filters( ) .../class-wp-hook.php:310 9 0.3157 26692960 gutenberg_editor_scripts_and_styles( ) .../class-wp-hook.php:286 10 0.3669 27449656 array_reduce ( ) .../client-assets.php:1189 11 0.3900 27836864 gutenberg_preload_api_request( ) .../client-assets.php:1189 12 0.3900 27838424 rest_do_request( ) .../client-assets.php:676 13 0.3900 27838424 WP_REST_Server->dispatch( ) .../rest-api.php:390 14 0.3922 27928912 WP_REST_Posts_Controller->get_item( ) .../class-wp-rest-server.php:936 15 0.3923 27929360 WP_REST_Posts_Controller->prepare_item_for_response( ) .../class-wp-rest-posts-controller.php:465 16 0.3942 27945408 apply_filters( ) .../class-wp-rest-posts-controller.php:1498 17 0.3942 27945808 WP_Hook->apply_filters( ) .../plugin.php:203 18 0.3947 27947688 beans_child_modify_post_content( ) .../class-wp-hook.php:286

Code on specific lines:

line 44        has_excerpt() ? get_the_excerpt() : wp_trim_words( $content, 40, '...' ),
line 45        beans_post_more_link()
line 46    );
line 47 }

Tested offline using Desktop Server and online using wpbeansframework.com.

christophherr commented 6 years ago

So with Gutenberg, you have to avoid the is_singular() check in the custom excerpt code. Only run it on archives / the front-page, or when you are not in a Rest request.