0

i was following a youtube tutorial of how to make wordpress admin option but stuck at a point i am trying to activate or show a metabox on all pages only when a contact form setting is activated from admin but so far nothing shows

here is my custom-post-type.php code

<?php 

/*
@package sunsettheme
  =============================
       THEME CUSTOM POST TYPES
  =============================
*/

$contact = get_option('activate_contact');


if(@$contact ==1){
    add_action( 'init', 'sunset_contact_custom_post_type' );
    add_filter('manage_sunset-contact_posts_columns','sunset_set_contact_columns');
    add_action('manage_sunset-contact_posts_custom_column','sunset_contact_custom_column',10,2);
    
    add_action('add_meta_boxes','sunset_contact_add_meta_box');
}

/*Contact Custom Post */

function sunset_contact_custom_post_type(){
    $labels = array(
     'name' => 'Messages',
     'singular_name' => 'Message',
     'menu_name' => 'Messages',
     'name_admin_bar' => 'Message',
    );
    $args = array(
      'labels'   => $labels,
      'show_ui'  => true,
      'show_in_menu' => true,
      'capability_type'=> 'post',
      'hierarchical' => false,
      'menu_position' => 26,
      'menu_icon' => 'dashicons-email-alt',
      'supports' => array('title','editor','author'),
      
    );
    register_post_type('sunset-contact',$args);
}
//Contact metaboxes
function sunset_contact_add_meta_box(){
    add_meta_box('contact_email','User Email','sunset_contact_email_callback','sunset-contact','side');
}

function sunset_contact_email_callback($post){
    //wp_nonce_field( $action, string $name, bool $referer,bool $echo )
    wp_nonce_field('sunset_save_email_data','sunset_contact_email_meta_box_nonce');
    
    //get_post_meta($post_id,$key,$single)
    $value = get_post_meta($post->ID,'_contact_email_value_key',true);
    
    echo'<label for="sunset_contact_email_field">User Email Address</label>';
    echo'<input type="email" id="sunset_contact_email_field" name="sunset_contact_email_field" value="'.esc_attr($value).'" size="25"/>';
}

and here is function-admin.php code

function sunset_custom_settings(){
    
      
      //Contact formoptions
      register_setting('sunset-contact-options','activate_contact');
      //section for contact form
      //add_settings_section( string $id, string $title, callable $callback, string $page );
      add_settings_section('sunset-contact-section','Contact form','sunset_contact_section','alecaddd_sunset_theme_contact');
      
      add_settings_field('activate-form', 'Activate Contact Form', 'sunset_activate_contact', 'alecaddd_sunset_theme_contact', 'sunset-contact-section');
      
}
//call back for sunset_theme_options
function sunset_theme_options(){
    echo 'Activate and Deactivate sepcific Theme Option Support Options';
}

//call back for sunset_contact_section
function sunset_contact_section(){
    echo 'Activate and Deactivate the Built in Contact form';
}
//activate contact
function sunset_activate_contact(){
    $options = get_option('activate_contact');
    //@ is isset here
        $checked = ( @$options ==1 ? 'checked' : '');
        echo '<label><input type="checkbox" id="custom_header" name="activate_contact" value="1"'.$checked.'></label>';
}

and no metabox shows here is screenshot enter image description here

enter image description here

any help will be appreciated, thanks

1
  • ok weird but after deleting and re adding sunset-contact here //Contact metaboxes function sunset_contact_add_meta_box(){ add_meta_box('contact_email','User Email','sunset_contact_email_callback','sunset-contact','normal'); } it now shows – Sash_007 Oct 28 '20 at 2:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.