Skip to content

Instantly share code, notes, and snippets.

@cdsaenz
Created January 14, 2025 21:18
Show Gist options
  • Save cdsaenz/48d07cb43ab57ebc0e55e92601427ad6 to your computer and use it in GitHub Desktop.
Save cdsaenz/48d07cb43ab57ebc0e55e92601427ad6 to your computer and use it in GitHub Desktop.
trying no empty p tags
<?php
function hdbc_page_header( $atts, $content = '' ) {
// get the current URL params array.
$url_params_raw = hdbc_get_url_params( true );
$url_params = $url_params_raw;
// remove the base slug.
unset( $url_params[0] );
// get the URL parameters for this page.
$url_params_string = implode( '-', $url_params );
// if we don't have any url parameters.
if ( empty( $url_params_string ) ) {
return '';
}
// query for a content header post with the same URL parameters as the slug.
$content_header = new WP_Query(
[
'post_type' => 'hd_bc_content',
'name' => $url_params_string,
'posts_per_page' => 1,
]
);
// buffer output.
ob_start();
// if we have a content header.
if ( $content_header->have_posts() ) {
// loop through the content header.
while ( $content_header->have_posts() ) {
// setup the post data.
$content_header->the_post();
// output the edit link - logged in users only.
edit_post_link( __( 'Edit header', 'hd-browse-content' ), '', '', get_the_ID(), 'hdbc-button hdbc-button__edit-header' );
// output the content.
the_content();
}
// reset the post data.
wp_reset_postdata();
} else {
// if the current user is logged in and can create a post.
if ( is_user_logged_in() && current_user_can( 'publish_posts' ) ) {
// enqueue the scripts.
wp_enqueue_script( 'hdbc-scripts' );
// create the current link with the create header query arg.
echo '<button data-header-name="' . esc_attr( $url_params_string ) . '" data-site-url="' . esc_url( home_url() ) . '" class="hdbc-button hdbc-button__create-header">' . esc_html__( 'Create Custom Header', 'hd-browse-content' ) . '</button>';
}
echo '<h1 class="hdbc-page-title">' . wp_kses_post( hdbc_generate_page_title_from_url_params() ) . '</h1>';
echo wp_kses_post( hdbc_generate_intro_description_from_url_params() );
}
$output = '<div style="border: 1px solid red; padding: 24px;" class="hdbc-header">' . ob_get_clean() . '</div>';
// return the output.
return $output;
}
add_shortcode( 'hdbc_page_header', 'hdbc_page_header', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment