I have a plugin that writes metaboxes to certain pages. It writes the metaboxes fine and everything works as should...until I save. The save_post is not getting called, and I can't figure out why. Here is the code in my __Construct
class Plg_Admin_Meta {
public static function start() {
return new self();
}
public function __construct() {
$this->plugin_url = WP_CONTENT_URL . '/plugins/' . basename(dirname(__FILE__));
$this->plugin_dir = dirname(__FILE__);
add_action('admin_init', array($this, '_admin_init'));
add_action('save_post', array($this, '_save_post'), 99);
}
The metaboxes are made from custom fields, and if I edit a custom field text area it saves fine, but I want it to save the data entered from the metabox instead. For whatever reason the save_post will not fire. I tried putting a die(var_dump($_POST)) at the start of my _save_post function and it isn't even firing.
Here is my _save_post function
public function _save_post($post_id) {
//die(dump_var($_POST));
update_post_meta($post_id, 'content__service_overviews', "test2");
// Post meta isn't sent for autosaves
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
}
Plg_Admin_Meta::start();
As you can see I am just manually trying to update a field right now, but the function isn't even getting called.
EDIT: Requested entire class:
class Plg_Admin_Meta {
public static function start() {
return new self();
}
public function __construct() {
$this->plugin_url = WP_CONTENT_URL . '/plugins/' . basename(dirname(__FILE__));
$this->plugin_dir = dirname(__FILE__);
$this->prefix = 'content__';
$this->page_fields = array(
'site' => array(
'textarea:slideshow',
'text:address',
'text:phone',
'text:fax',
'text:twitter_link',
'text:weather_link',
'text:meta_description',
'text:meta_keywords',
'text:google_analytics_account_number',
'text:google_site_verification_code'
),
'home' => array(
'textarea:about_enernex',
'textarea:introducing_utility_stats',
'textarea:recent_content',
'textarea:workshop_button'
),
'about' => array(
'textarea:what_we_do_photo',
'textarea:what_we_do',
'textarea:our_mission_photo',
'textarea:our_mission',
'textarea:get_to_know_us_photo',
'textarea:get_to_know_us_principals',
'textarea:get_to_know_us_staff'
),
'careers' => array(
'textarea:why_enernex_photo',
'textarea:why_enernex',
'textarea:support_our_mission_photo',
'textarea:support_our_mission',
'textarea:the_innovation_valley_photo',
'textarea:the_innovation_valley_our_location'
),
'contact' => array(
'textarea:photo',
'textarea:contact_information',
'textarea:map_image',
'textarea:directions'
),
'employees' => array(
'text:title',
'textarea:text',
'textarea:photo'
),
'support' => array(
'textarea:announcing_utility_stats',
'textarea:faq',
'textarea:utility_ownership_key',
'textarea:support'
),
'newsroom' => array(
'textarea:service_overviews',
'textarea:media_contacts',
'textarea:industry_resources_and_related_links'
),
'services' => array(
'textarea:main_text',
'textarea:power_systems_consulting_photo',
'textarea:smart_grid_engineering_photo',
'textarea:smart_grid_labs_photo',
'textarea:workshops_photo'
),
'iec-workshop' => array(
'textarea:first_photo',
'textarea:course_overview',
'textarea:topics_covered',
'textarea:our_instructors',
'textarea:workshop_dates',
'textarea:who_should_attend',
'textarea:key_benefits',
'textarea:schedule_location_and_cost',
'textarea:second_photo',
'textarea:location'
),
'cyber-security' => array(
'textarea:meet_the_team',
'text:media_kit_download_link'
)
);
$this->post_fields = array(
'news' => array(
'textarea:overview',
'textarea:story'
),
'press' => array(
'textarea:overview',
'textarea:story'
),
'whitepapers' => array(
'textarea:overview',
'textarea:uploads'
),
'areas' => array(
'textarea:overview',
'textarea:uploads',
'textarea:projects'
),
'links' => array(
'text:URL'
),
'jobs' => array(
'textarea:basic_function',
'textarea:education_and_experience',
'textarea:skills',
'textarea:duties_and_responsibilities',
'textarea:contact_information',
'textarea:notes'
),
'employees' => array(
'text:position',
'text:department',
'text:year_joined',
'text:power_industry_years',
'text:education',
'text:expertise',
'text:phone',
'text:email_address',
'textarea:biography',
'textarea:headshot'
),
'blog' => array(
'textarea:author',
'textarea:content'
),
'announcements' => array(
'text:link_to_announcement',
'textarea:content'
)
);
add_action('admin_init', array($this, '_admin_init'));
add_action('save_post', array($this, '_save_post'), 99, 2);
}
public function _admin_init() {
$post_id = isset($_REQUEST['post']) ? $_REQUEST['post'] : false;
if ($post_id) {
$content = get_post($post_id);
$slug = $content->post_name;
$post_type = $content->post_type;
if (isset($post_type)) {
switch($post_type) {
case 'page':
$this->_create_metaboxes('page', $this->page_fields[$slug]);
break;
case 'post':
$category = get_the_category($post_id);
$slug = $category[0]->slug;
$this->_create_metaboxes('post', $this->post_fields[$slug]);
break;
}
}
}
}
public function _create_metabox_content($post, $metabox) {
$post_id = $post->ID;
$meta_key = $metabox['args']['meta_key'];
$meta_value = get_post_meta($post_id, $meta_key, true);
switch ($metabox['args']['type']) {
case 'text':
?>
<input type="text" name="<?php echo $meta_key; ?>" value="<?php echo $meta_value; ?>" style="font-family:'Georgia', 'Times New Roman', 'Bistream Charter', 'Times', 'serif';font-size: 13px; line-height: 19px; color: #333; width: 100%;" />
<?php
break;
case 'textarea':
wp_editor(
$meta_value,
$meta_key,
array(
'textarea_name' => $meta_key,
'textarea_rows' => 4,
'tinymce' => array(
'content_css' => $this->plugin_url . '/tinymce.css'
)
)
);
break;
}
}
public function _create_metaboxes($post_type, $fields_array) {
if (!empty($fields_array)) {
wp_enqueue_style('admin', $this->plugin_url . '/admin.css');
foreach ($fields_array as $field) {
$tokens = explode(':', $field);
$type = $tokens[0];
$meta_key = $this->prefix . $tokens[1];
$id = 'metabox_' . $tokens[1];
$title = ucwords(preg_replace('/\_/', ' ', $tokens[1]));
add_meta_box(
$id,
$title,
array($this, '_create_metabox_content'),
$post_type,
'normal',
'high',
array(
'meta_key' => $meta_key,
'type' => $type
)
);
}
}
}
public function _save_post($post_id) {
//die(var_dump($_POST));
// Post meta isn't sent for autosaves
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
update_post_meta($post_id, 'content__service_overviews', "test2");
if (isset($_REQUEST['post_ID'])) {
foreach ($_REQUEST as $meta_key => $meta_value) {
if (strstr($meta_key, $this->prefix)) {
//update_post_meta(intval($_REQUEST['post_ID']), meta_key, meta_value);
}
}
}
}
}
Plg_Admin_Meta::start();