Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active August 29, 2015 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/7cbf3e7c755bc58756bf to your computer and use it in GitHub Desktop.
Save tommcfarlin/7cbf3e7c755bc58756bf to your computer and use it in GitHub Desktop.
[WordPress] Determine if the current post type is in a list of selected, supported post types.
<?php
add_action( 'admin_menu', 'acme_add_post_types_menu' );
/**
* Adds a new submenu item to the Settings Menu that displays the list of post types supported
* by the current theme.
*
*/
function acme_add_post_types_menu() {
add_options_page(
'Current Post Types',
'Current Post Types',
'manage_options',
'acme-post-types',
'acme_options_page'
);
}
<?php
add_action( 'admin_init', 'acme_add_post_types' );
/**
* Defines the section, settings, and fields used to render the post types
* supported by this theme.
*
*/
function acme_add_post_types() {
add_settings_section(
'acme-post-types',
'Post Types',
'acme_display_post_types' ,
'acme-post-types'
);
add_settings_field(
'acme-post-types',
'Available Post Types',
'acme_render_post_types',
'__return_null',
'acme-post-types'
);
register_setting(
'acme-post-settings',
'acme-post-types'
);
}
<?php
/**
* Displays a list of checkboxes each of which corresponds to the post types
* supported by this this.
*
*/
function acme_display_post_types() {
$options = get_option( 'acme-post-types', array() );
$post_types = get_post_types( array(), 'objects' );
$html = "<ul>";
foreach ( $post_types as $post_type ) {
$post_type_label = $post_type->labels->singular_name;
$current_value = isset( $options[ $post_type_label ] ) ? $options[ $post_type_label ] : '';
$html .= "<li>";
$html .= "<label for='acme-post-types'>";
$html .= "<input type='checkbox' id='acme-post-types' name='acme-post-types[$post_type_label]' value='" . esc_attr( $post_type_label ) . "'" . checked( $current_value, $post_type_label, FALSE ) . " />";
$html .= esc_html( $post_type_label );
$html .= "</label>";
$html .= "</li>";
}
$html .= "</ul>";
echo $html;
}
<?php
add_action( 'the_content', 'acme_is_supported_post_type' );
/**
* Determines if the current post type has been selected in the admin and, if so,
* will append a string to the footer of the content.
*
* @param string The content for the current post type.
* @return string The content that's adjusted (or not) based on the selected settings.
*/
function acme_is_supported_post_type( $content ) {
$post_types = get_option( 'acme-post-types', array() );
if ( in_array( ucwords( get_post_type() ), $post_types ) ) {
$content .= '[ This post type has been selected. ]';
}
return $content;
}
<?php
/**
* Renders the sections and fields as defined using the Settings API.
*
* This is what allows users to check (or uncheck) the post types that
* they want supported in the functionality demonstrated in this code.
*
*/
function acme_options_page() {
?>
<div class="wrap">
<h2>Post Types</h2>
<form action="options.php" method="post">
<?php do_settings_sections( 'acme-post-types' ); ?>
<?php settings_fields( 'acme-post-settings' ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment