bp_theme_compat_reset_post( array $args = array() )

Populate various WordPress globals with dummy data to prevent errors.

Description

This dummy data is necessary because theme compatibility essentially fakes WordPress into thinking that there is content where, in fact, there is none (at least, no WordPress post content). By providing dummy data, we ensure that template functions – things like is_page() – don’t throw errors.

Parameters

$args

(Optional) Array of optional arguments. Arguments parallel the properties of WP_Post; see that class for more details.

Default value: array()

Source

File: bp-core/bp-core-theme-compatibility.php

463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
function bp_theme_compat_reset_post( $args = array() ) {
    global $wp_query, $post;
 
    // Switch defaults if post is set.
    if ( isset( $wp_query->post ) ) {
        $dummy = wp_parse_args( $args, array(
            'ID'                    => $wp_query->post->ID,
            'post_status'           => $wp_query->post->post_status,
            'post_author'           => $wp_query->post->post_author,
            'post_parent'           => $wp_query->post->post_parent,
            'post_type'             => $wp_query->post->post_type,
            'post_date'             => $wp_query->post->post_date,
            'post_date_gmt'         => $wp_query->post->post_date_gmt,
            'post_modified'         => $wp_query->post->post_modified,
            'post_modified_gmt'     => $wp_query->post->post_modified_gmt,
            'post_content'          => $wp_query->post->post_content,
            'post_title'            => $wp_query->post->post_title,
            'post_excerpt'          => $wp_query->post->post_excerpt,
            'post_content_filtered' => $wp_query->post->post_content_filtered,
            'post_mime_type'        => $wp_query->post->post_mime_type,
            'post_password'         => $wp_query->post->post_password,
            'post_name'             => $wp_query->post->post_name,
            'guid'                  => $wp_query->post->guid,
            'menu_order'            => $wp_query->post->menu_order,
            'pinged'                => $wp_query->post->pinged,
            'to_ping'               => $wp_query->post->to_ping,
            'ping_status'           => $wp_query->post->ping_status,
            'comment_status'        => $wp_query->post->comment_status,
            'comment_count'         => $wp_query->post->comment_count,
            'filter'                => $wp_query->post->filter,
 
            'is_404'                => false,
            'is_page'               => false,
            'is_single'             => false,
            'is_archive'            => false,
            'is_tax'                => false,
        ) );
    } else {
        $dummy = wp_parse_args( $args, array(
            'ID'                    => -9999,
            'post_status'           => 'public',
            'post_author'           => 0,
            'post_parent'           => 0,
            'post_type'             => 'page',
            'post_date'             => 0,
            'post_date_gmt'         => 0,
            'post_modified'         => 0,
            'post_modified_gmt'     => 0,
            'post_content'          => '',
            'post_title'            => '',
            'post_excerpt'          => '',
            'post_content_filtered' => '',
            'post_mime_type'        => '',
            'post_password'         => '',
            'post_name'             => '',
            'guid'                  => '',
            'menu_order'            => 0,
            'pinged'                => '',
            'to_ping'               => '',
            'ping_status'           => '',
            'comment_status'        => 'closed',
            'comment_count'         => 0,
            'filter'                => 'raw',
 
            'is_404'                => false,
            'is_page'               => false,
            'is_single'             => false,
            'is_archive'            => false,
            'is_tax'                => false,
        ) );
    }
 
    // Bail if dummy post is empty.
    if ( empty( $dummy ) ) {
        return;
    }
 
    // Set the $post global.
    $post = new WP_Post( (object) $dummy );
 
    // Copy the new post global into the main $wp_query.
    $wp_query->post       = $post;
    $wp_query->posts      = array( $post );
 
    // Prevent comments form from appearing.
    $wp_query->post_count = 1;
    $wp_query->is_404     = $dummy['is_404'];
    $wp_query->is_page    = $dummy['is_page'];
    $wp_query->is_single  = $dummy['is_single'];
    $wp_query->is_archive = $dummy['is_archive'];
    $wp_query->is_tax     = $dummy['is_tax'];
 
    // Clean up the dummy post.
    unset( $dummy );
 
    /**
     * Force the header back to 200 status if not a deliberate 404.
     *
     */
    if ( ! $wp_query->is_404() ) {
        status_header( 200 );
    }
 
    // If we are resetting a post, we are in theme compat.
    bp_set_theme_compat_active( true );
 
    // If we are in theme compat, we don't need the 'Edit' post link.
    add_filter( 'get_edit_post_link', 'bp_core_filter_edit_post_link', 10, 2 );
}

Changelog

Changelog
Version Description
BuddyPress 1.7.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.