Created
July 23, 2014 23:07
-
-
Save atwellpub/ebe7e8baf59a7651652a to your computer and use it in GitHub Desktop.
How to add settings to the 'Advanced Settings' metabox within calls to action.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
*This class hooks call to action settings into the 'Advanced Settings' metabox available when editing a call to action | |
*/ | |
if ( !class_exists('CTA_Extension_Metaboxes') ) { | |
class CTA_Extension_Metaboxes | |
{ | |
/** | |
* Initialize class | |
*/ | |
function __construct() | |
{ | |
self::load_hooks(); | |
} | |
/** | |
* Loads WordPress Hooks and Filters | |
*/ | |
public static function load_hooks() | |
{ | |
/* settings for wordpress-call-to-action cpt */ | |
add_filter( "wp_cta_extension_data", array( __CLASS__ , "add_advanced_settings") , 10 , 1 ); | |
/* handler for empty inputs */ | |
add_filter( "save_post", array( __CLASS__ , "save_data_fallback") , 10 , 1 ); | |
} | |
/** | |
* Intercepts $wp_cta_data which contains all the settings and template data and injects our new advanced settings into the data array | |
* | |
* @param ARRAY $wp_cta_data variable containg data related to Call to Action environment | |
* | |
* @returns ARRAY modified version of $wp_cta_data | |
*/ | |
public static function add_advanced_settings( $wp_cta_data ) | |
{ | |
$parent_key = 'wp-cta'; //leave this alone | |
/* Add setting to $wp_cta_data array */ | |
$wp_cta_data[$parent_key]['settings']['global-placements-header'] = array( | |
'datatype' => 'setting', | |
'region' => 'advanced', | |
'description' => '<h3>Global Placements</h3>', | |
'id' => 'global-placements-header', | |
'type' => 'html-block' | |
); | |
/* Add setting to $wp_cta_data array */ | |
$wp_cta_data[$parent_key]['settings']['post-type-placements-header'] = array( | |
'datatype' => 'setting', | |
'region' => 'advanced', | |
'description' => ' <i>Placement by Post Type</i>', | |
'id' => 'post-type-placements-header', | |
'type' => 'html-block', | |
'global' => true //setting global to true will make this get_post_meta() id to always be $partent_key + 'id' eg: wp-cta-post-type' where as if this was set to false the store key would contain the variation id at the end like wp-cta-post-type-1. Use this to define settings that will be applied to all call to action variations. | |
); | |
/* build option array of post types */ | |
$post_types= get_post_types( array('public'=>true) ,'names'); | |
$options = array(); | |
foreach ($post_types as $post_type ) | |
{ | |
if ( in_array( $post_type , array( 'revision','nav_menu_item' , 'attachment' , 'wp-call-to-action' ) ) ) { | |
continue; | |
} | |
$options[ $post_type ] = $post_type; | |
} | |
/* Add setting to $wp_cta_array using the options generate above as a dropdown. */ | |
$wp_cta_data[$parent_key]['settings']['post-types'] = array( | |
'datatype' => 'setting', | |
'region' => 'advanced', | |
'label' => 'Post Types', | |
'description' => 'Place on these post types', | |
'id' => 'post-type', | |
'type' => 'multiselect', | |
'options' => $options, | |
'global' => true | |
); | |
/* Add setting to $wp_cta_array */ | |
$wp_cta_data[$parent_key]['settings']['cta_content_placement'] = array( | |
'data_type' => 'setting', | |
'region' => 'advanced', | |
'label' => 'Placement', | |
'description' => "Where would you like to insert the CTA on this page?", | |
'id' => 'content-placement', | |
'type' => 'dropdown', | |
'default' => 'off', | |
'options' => array( | |
'above'=>'Above Content', | |
'middle' => 'Middle of Content', | |
'below' => 'Below Content', | |
'widget_1' => 'Use Dynamic Sidebar Widget' | |
), | |
'context' => 'normal', | |
'class' => 'cta-per-page-option', | |
'global' => true //setting global to true tells this setting to be applied to all variations and saves the setting | |
); | |
/* send the data array back with the new settings */ | |
return $wp_cta_data; | |
} | |
/** | |
* This hooked method will delete the wp-cta-post-type meta pair if it's empty. | |
*/ | |
public static function save_data_fallback( $cta_id ) { | |
global $post; | |
unset($_POST['post_content']); | |
if ( wp_is_post_revision( $cta_id ) ) { | |
return; | |
} | |
if ( !isset($_POST['post_type']) || $_POST['post_type'] != 'wp-call-to-action' ) { | |
return; | |
} | |
if (!isset($_POST['wp-cta-post-type'])) { | |
delete_post_meta( $cta_id , 'wp-cta-post-type' ); | |
} | |
} | |
} | |
/** Load the class on fileload */ | |
$class['CTA_Extension_Metaboxes'] = new CTA_Extension_Metaboxes(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment