Skip to content

Instantly share code, notes, and snippets.

@bryanwillis
Last active November 2, 2015 06:02
Show Gist options
  • Save bryanwillis/752e6c5fccec8c3e4389 to your computer and use it in GitHub Desktop.
Save bryanwillis/752e6c5fccec8c3e4389 to your computer and use it in GitHub Desktop.
//uses single template but not as good for theme compatability
<?php
function custom_template_snippet_post_type_plugin( $single ) {
global $wp_query, $post;
if ($post->post_type == "sdr_snippet"){
// if in subfolder
$template = plugin_dir_path( __FILE__ ) . 'templates/single-snippet.php';
// if in plugin root directory folder
//$template = plugin_dir_path( __FILE__ ) . '/single-snippet.php';
// directory folder file
//$template = dirname( __FILE__ ) . '/single-snippet.php';
if(file_exists( $template ))
return $template;
}
return $single;
}
add_filter('single_template', 'custom_template_snippet_post_type_plugin');
?>
// THEN SINGLE WOULD LOOK SOMETHING LIKE THIS
<?php
function print_snippet_single_post() {
$language = sdr_snippet_get_language( get_the_ID() );
$html = '';
$html .= '<div class="sdr-snippet-container" data-language="' . esc_attr( $language ) . '" data-show-lines="true" data-height="0">';
$html .= '<pre class="sdr-snippet-box">';
$html .= esc_html( get_the_content() );
$html .= sdr_enqueue_frontend();
$html .= '</pre>';
$html .= '</div>';
return $html;
}
get_header();
echo print_snippet_single_post();
get_footer();
?>
<?php
if ( $post = get_post( $page_id ) ) {
setup_postdata( $post ); // "posts" page is now current post for most template tags
the_content();
wp_reset_postdata(); // So everything below functions as normal
}
// THIS WAY WORKS FOR SURE
<?php
function print_snippet_single_post($content) {
$postid = get_the_ID();
$post_type = get_post_type( $postid );
if ( is_singe( $postid ) ){
if (have_posts()) :
the_post();
// echo sdr_do_shortcode_snippet( $atts );
$language = sdr_snippet_get_language( $postid );
$html = '';
$html .= '<div class="sdr-snippet-container" data-language="' . esc_attr( $language ) . '" data-show-lines="true" data-height="0">';
$html .= '<pre class="sdr-snippet-box">';
$html .= esc_html( get_the_content() );
$html .= '</pre>';
$html .= '</div>';
if ( $post_type == "sdr_snippet") {
return $html;
}
else {
return $content;
}
endif;
}
// wp_reset_postdata();
}
add_filter( 'the_content', 'print_snippet_single_post', 10 );
function print_snippet_single_post($content) {
$post_type = get_post_type( get_the_ID() );
$language = sdr_snippet_get_language( get_the_ID() );
$html = '';
$html .= '<div class="sdr-snippet-container" data-language="' . esc_attr( $language ) . '" data-show-lines="true" data-height="0">';
$html .= '<pre class="sdr-snippet-box">';
$html .= esc_html( get_the_content() );
$html .= '</pre>';
$html .= '</div>';
if ( $post_type == "sdr_snippet") {
return $html;
}
else {
return $content;
}
}
add_filter( 'the_content', 'print_snippet_single_post', 10 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment