• Resolved jojaba

    (@jojaba)


    Hello,

    I’ve created a little plugin to set a new post type that I named ‘event’. It works fine when I’m connected as super-admin, but not when I try to log in as an other role user (author, admin,…). For these last users all is ok but the event creation window where the metaboxes doesn’t display. Here’s the code :

    <?php
    
    /*
    Plugin Name: VSAW - Very Simple Agenda for WordPress
    Plugin URI: http://perso.jojaba.fr/
    Description: Provides a very simple way to define events.
    Author: Jojaba
    Version: 1.0
    Author URI: http://perso.jojaba.fr/
    */
    
    /**
     * Custom posts for agenda called event
     */
     function events_custom_init() {
      $labels = array(
        'name'               => 'Évènements',
        'singular_name'      => 'Évènement',
        'add_new'            => 'Nouveau',
        'add_new_item'       => 'Ajouter un évènement',
        'edit_item'          => 'Modifier l\'évènement',
        'new_item'           => 'Nouvel évènement',
        'all_items'          => 'Tous les évènements',
        'view_item'          => 'Afficher l\'évènement',
        'search_items'       => 'Rechercher des évènements',
        'parent_item_colon'  => '',
        'menu_name'          => 'Évènements'
      );
    
      $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'menu_position' 	 => 5,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'agenda' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'supports'           => array( 'title' )
      );
    
      register_post_type( 'event', $args );
    }
    add_action( 'init', 'events_custom_init' );
    
    /**
     * Adds a meta box to the post editing screen
     */
    function vsaw_custom_meta() {
    	add_meta_box( 'vsaw_meta', __( 'Informations sur l\'évènement', 'vsaw-textdomain' ), 'vsaw_meta_callback', 'event', 'high' );
    }
    add_action( 'add_meta_boxes', 'vsaw_custom_meta' );
    
    /**
     * Outputs the content of the meta box
     */
    function vsaw_meta_callback( $post ) {
    	wp_nonce_field( basename( __FILE__ ), 'vsaw_nonce' );
    	$vsaw_stored_meta = get_post_meta( $post->ID );
    	?>
    
    	<p class="start_date">
    		<label for="meta-start-date"><?php _e( 'Début de l\'évènement', 'vsaw-textdomain' )?></label><br>
    		<input required type="text" name="meta-start-date" id="meta-start-date" value="<?php if ( isset ( $vsaw_stored_meta['meta-start-date'] ) ) echo $vsaw_stored_meta['meta-start-date'][0]; ?>" />
    	</p>
    
    	<p class="end_date">
    		<label for="meta-end-date"><?php _e( 'Fin de l\'évènement', 'vsaw-textdomain' )?></label><br>
    		<input type="text" name="meta-end-date" id="meta-end-date" value="<?php if ( isset ( $vsaw_stored_meta['meta-end-date'] ) ) echo $vsaw_stored_meta['meta-end-date'][0]; ?>" />
    	</p>
    
    	<p class="location">
    		<label for="meta-loc"><?php _e( 'Lieu', 'vsaw-textdomain' )?></label><br>
    		<input required type="text" name="meta-loc" id="meta-loc" value="<?php if ( isset ( $vsaw_stored_meta['meta-loc'] ) ) echo $vsaw_stored_meta['meta-loc'][0]; ?>" />
    	</p>
    
    	<p class="url">
    		<label for="meta-url"><?php _e( 'URL (facultatif)', 'vsaw-textdomain' )?></label><br>
    		<input type="text" name="meta-url" id="meta-url" value="<?php if ( isset ( $vsaw_stored_meta['meta-url'] ) ) echo $vsaw_stored_meta['meta-url'][0]; ?>" />
    	</p>
    
    	<script>
    	/* The date picker displaying */
        jQuery(document).ready(function(){
        		jQuery("#meta-start-date, #meta-end-date").on("click", function() {
        				jQuery(this).pickadate();
        		});
        });
        </script> 
    
    	<?php
    }
    
    /**
     * Saves the custom meta input
     */
    function vsaw_meta_save( $post_id ) {
    
    	// Checks save status
    	$is_autosave = wp_is_post_autosave( $post_id );
    	$is_revision = wp_is_post_revision( $post_id );
    	$is_valid_nonce = ( isset( $_POST[ 'vsaw_nonce' ] ) && wp_verify_nonce( $_POST[ 'vsaw_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';
    
    	// Exits script depending on save status
    	if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
    		return;
    	}
    
    	// Checks for start date input and sanitizes/saves if needed
    	if( isset( $_POST[ 'meta-start-date' ] ) ) {
    		update_post_meta( $post_id, 'meta-start-date', sanitize_text_field( $_POST[ 'meta-start-date' ] ) );
    	}
    
    	if( isset( $_POST[ 'meta-start-date_submit' ] ) ) {
    		update_post_meta( $post_id, 'meta-start-date_submit', sanitize_text_field( $_POST[ 'meta-start-date_submit' ] ) );
    	}
    
    	// Checks for end date input and sanitizes/saves if needed
    	if( isset( $_POST[ 'meta-end-date' ] ) ) {
    		update_post_meta( $post_id, 'meta-end-date', sanitize_text_field( $_POST[ 'meta-end-date' ] ) );
    	}
    
    	if( isset( $_POST[ 'meta-end-date_submit' ] ) ) {
    		update_post_meta( $post_id, 'meta-end-date_submit', sanitize_text_field( $_POST[ 'meta-end-date_submit' ] ) );
    	}
    
    	// Checks for location input and sanitizes/saves if needed
    	if( isset( $_POST[ 'meta-loc' ] ) ) {
    		update_post_meta( $post_id, 'meta-loc', sanitize_text_field( $_POST[ 'meta-loc' ] ) );
    	}
    
    	// Checks for url input and sanitizes/saves if needed
    	if( isset( $_POST[ 'meta-url' ] ) ) {
    		update_post_meta( $post_id, 'meta-url', sanitize_text_field( $_POST[ 'meta-url' ] ) );
    	}
    
    }
    add_action( 'save_post', 'vsaw_meta_save' );
    
    /**
     * Adds the meta box stylesheet when appropriate
     */
    function vsaw_admin_styles(){
    	global $typenow;
    	if( $typenow == 'event' ) {
    		wp_enqueue_style( 'vsaw_meta_box_styles', plugin_dir_url( __FILE__ ) . 'vsaw.css' );
    	}
    }
    add_action( 'admin_print_styles', 'vsaw_admin_styles' );
    
    /**
     * Loads the date picker js and css
     */
    function vsaw_date_enqueue() {
    	global $typenow;
    	if( $typenow == 'event' ) {
    		wp_enqueue_style( 'vsaw_picker_style', plugin_dir_url( __FILE__ ) . 'pickadate/lib/themes/default.css' );
    		wp_enqueue_style( 'vsaw_date_style', plugin_dir_url( __FILE__ ) . 'pickadate/lib/themes/default.date.css' );
    		wp_enqueue_script( 'vsaw-box-picker-js', plugin_dir_url( __FILE__ ) . 'pickadate/lib/picker.js' );
    		wp_enqueue_script( 'vsaw-box-date-js', plugin_dir_url( __FILE__ ) . 'pickadate/lib/picker.date.js' );
    		wp_enqueue_script( 'vsaw-box-legacy-js', plugin_dir_url( __FILE__ ) . 'pickadate/lib/legacy.js' );
    		wp_enqueue_script( 'vsaw-box-fr-js', plugin_dir_url( __FILE__ ) . 'pickadate/lib/translations/fr_FR.js' );
    	}
    }
    add_action( 'admin_enqueue_scripts', 'vsaw_date_enqueue' );

    Problem of capabilities ? (I thought, they are inherited from the ‘post’ type…)

    Thanks in advance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter jojaba

    (@jojaba)

    I just tried to install the plugin into another WordPress site anywhere else. The metabox doesn’t display even for the super admin…
    Something wrong with the printing of the metabox?

    /**
     * Adds a meta box to the post editing screen
     */
    function vsaw_custom_meta() {
    	add_meta_box( 'vsaw_meta', __( 'Informations sur l\'évènement', 'vsaw-textdomain' ), 'vsaw_meta_callback', 'event', 'high' );
    }
    add_action( 'add_meta_boxes', 'vsaw_custom_meta' );
    
    /**
     * Outputs the content of the meta box
     */
    function vsaw_meta_callback( $post ) {
    	wp_nonce_field( basename( __FILE__ ), 'vsaw_nonce' );
    	$vsaw_stored_meta = get_post_meta( $post->ID );
    	?>
    
    	<p class="start_date">
    		<label for="meta-start-date"><?php _e( 'Début de l\'évènement', 'vsaw-textdomain' )?></label><br>
    		<input required type="text" name="meta-start-date" id="meta-start-date" value="<?php if ( isset ( $vsaw_stored_meta['meta-start-date'] ) ) echo $vsaw_stored_meta['meta-start-date'][0]; ?>" />
    	</p>
    
    	<p class="end_date">
    		<label for="meta-end-date"><?php _e( 'Fin de l\'évènement', 'vsaw-textdomain' )?></label><br>
    		<input type="text" name="meta-end-date" id="meta-end-date" value="<?php if ( isset ( $vsaw_stored_meta['meta-end-date'] ) ) echo $vsaw_stored_meta['meta-end-date'][0]; ?>" />
    	</p>
    
    	<p class="location">
    		<label for="meta-loc"><?php _e( 'Lieu', 'vsaw-textdomain' )?></label><br>
    		<input required type="text" name="meta-loc" id="meta-loc" value="<?php if ( isset ( $vsaw_stored_meta['meta-loc'] ) ) echo $vsaw_stored_meta['meta-loc'][0]; ?>" />
    	</p>
    
    	<p class="url">
    		<label for="meta-url"><?php _e( 'URL (facultatif)', 'vsaw-textdomain' )?></label><br>
    		<input type="text" name="meta-url" id="meta-url" value="<?php if ( isset ( $vsaw_stored_meta['meta-url'] ) ) echo $vsaw_stored_meta['meta-url'][0]; ?>" />
    	</p>
    
    	<script>
    	/* The date picker displaying */
        jQuery(document).ready(function(){
        		jQuery("#meta-start-date, #meta-end-date").on("click", function() {
        				jQuery(this).pickadate();
        		});
        });
        </script> 
    
    	<?php
    }

    Thread Starter jojaba

    (@jojaba)

    Well got it to work !
    After hundreds reading agian and again, I finally saw I did a msitake in the metabox function :

    function vsaw_custom_meta() {
    	add_meta_box( 'vsaw_meta', __( 'Informations sur l\'évènement', 'vsaw-textdomain' ), 'vsaw_meta_callback', 'event', 'high' );
    }

    I forget a setting in this function :

    function vsaw_custom_meta() {
    	add_meta_box( 'vsaw_meta', __( 'Informations sur l\'évènement', 'vsaw-textdomain' ), 'vsaw_meta_callback', 'event', normal, 'high' );
    }

    😉

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘custom post type not available for all users…’ is closed to new replies.