• Resolved wp-lionel

    (@wp-lionel)


    Hello everyone.

    I made some code to create custom_post_type with a custom taxonomy.
    My code is working but there is a little bug…

    1/ If my code is in the functions.php file:
    When I create a term for my custom taxonomy, after validation the page is reloaded and my new term appears in the list.
    It’s OKay

    2/ If my code is in a plugin instead:
    When I create a term for my custom taxonomy, after validation the page is NOT reloaded, my new term DOES NOT appears in the list.
    If I reload manually the page, the new term then appears.

    I don’t understand why…
    Do I need another step when I make a plugin?

    Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter wp-lionel

    (@wp-lionel)

    here is the code if eventualy someone who wants to make test

    <?php
    /*
    Plugin Name: lg-vehicules
    Description: Déclare les types de contenus personnalisés "véhicules"
    Version: 1.0
    Author: lui même
    */
    
    /* fonction général flush rewrite   */
    function my_plugin_deactivate()  {
        flush_rewrite_rules();
    }
    
    // ========================================================================
    // ----------------------déclaration custom post type  véhicules-----------
    // ========================================================================
    
    function lgib_create_vehicules()  {
      $labels = array(
        'name' => 'Véhicules',
        'singular_name' => 'Véhicule',
        'add_new' => 'Ajouter',
        'add_new_item' => 'Ajouter un véhicule',
        'edit_item' => 'Modifier un véhicule',
        'new_item' => 'Nouveau Véhicule',
        'all_items' => 'Tous les Véhicules',
        'view_item' => 'Voir le véhicule',
        'search_items' => 'Chercher des véhicules',
        'not_found' => 'Aucun Véhicule trouvé',
        'not_found_in_trash' => 'Aucun Véhicule dans la corbeille',
        'menu_name' => 'Véhicules'
      );
    
      $args = array(
        'labels' => $labels,
        'description' => 'contient toutes les caractéristiques d\'un véhicule',
        'public' => true,
        'has_archive' => true,
        'show_ui' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'vehicule'),
        'capability_type' => 'post',
        'show_in_menu' => true,
        'show_in_admin_bar' => true,
        'menu_position' => 20,
        'menu_icon' => get_bloginfo('wpurl'). '/wp-content/plugins/vehicules/images/car-icon-16.png',
        'supports' => array('title', 'thumbnail', 'page-attributes', 'author'),
        'can_export' => true
      );
    
      register_post_type( 'lgib_vehicule', $args);
    }
    
    function vehicule_rewrite_flush()  {
      lgib_create_vehicules();
      flush_rewrite_rules();
    }
    
    add_action('init', 'lgib_create_vehicules');
    register_activation_hook(__FILE__, 'vehicule_rewrite_flush');
    register_deactivation_hook(__FILE__, 'my_plugin_deactivate');
    
    // ========================================================================
    // ------------ messages associés au  custom-post-type véhicule-----------
    // ========================================================================
    function lgib_vehicule_updated_messages( $messages ) {
      global $post, $post_ID;
    
      $messages['lgib_vehicule'] = array(
        0 => '', // Unused. Messages start at index 1.
    
        1 => sprintf( 'Véhicule mis à jour. <a href="%s">Voir le Véhicule</a>', esc_url( get_permalink($post_ID) ) ),
    
        2 => 'Champ personnalisé mis à jour',
        3 => 'Champ personnalisé supprimé',
        4 => 'Véhicule mis à jour',
        /* translators: %s: date and time of the revision */
        5 => isset($_GET['revision']) ? sprintf( __('Véhicule restored to revision from %s', 'your_text_domain'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
        6 => sprintf( __('véhicule published. <a href="%s">View book</a>', 'your_text_domain'), esc_url( get_permalink($post_ID) ) ),
        7 => __('Book saved.', 'your_text_domain'),
        8 => sprintf( __('Book submitted. <a target="_blank" href="%s">Preview book</a>', 'your_text_domain'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
        9 => sprintf( __('Book scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview book</a>', 'your_text_domain'),
          // translators: Publish box date format, see http://php.net/date
          date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
        10 => sprintf( __('Book draft updated. <a target="_blank" href="%s">Preview book</a>', 'your_text_domain'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
      );
    
      return $messages;
    }
    add_filter( 'post_updated_messages', 'lgib_vehicule_updated_messages' );
    
    // ==================================================== mise en place de l'aide
    function lgib_vehicule_add_help_text($contextual_help, $screen_id, $screen) {
        if ( 'lgib_vehicule' == $screen->id )  {
            $contextual_help = '<p>Ici l\'aide basique</p>';
        } else if ( 'edit-lgib_vehicule' == $screen->id ) {
            $contextual_help = '<p>aide (edit) </p>';
        } else if ( 'edit-lgib_type' == $screen->id ) {
            $contextual_help = '<p>aide edition du type (taxonomie) </p>';
        }
        return $contextual_help;
    }
    add_action( 'contextual_help', 'lgib_vehicule_add_help_text', 10, 3);
    
    // ================================================= onglets aide personnalisés
    function lgib_vehicule_custom_help_tab()  {
        global $post_ID;
        $screen = get_current_screen();
    
        if ( isset($_GET['post_type']) )  {
          $post_type = $_GET['post_type'];
        }  else  {
          $post_type = get_post_type( $post_ID );
        }
    
        if ( $post_type == 'lgib_vehicule') {
            $screen->add_help_tab( array(
                'id' => 'lgib_tab1',
                'title' => 'onglet supplémentaire',
                'content' => '<h3>ici un onglet supplémentaire</h3>'
            ));
        }
    
        if (is_taxonomy('lgib_type'))  {
            $screen->add_help_tab( array(
                'id' => 'lgib_type_help',
                'title' => 'Aide sur le type',
                'content' => '<h3>choisissez un type dans la liste</h3>'
            ));
        }
    }
    add_action( 'admin_head', 'lgib_vehicule_custom_help_tab');
    
    // ========================================================================
    // ------------ taxonomie associé au  custom-post-type véhicule -----------
    // ========================================================================
    function lgib_vehicule_taxonomy()  {
      $args = array(
          'label'   =>  'type',
          'query_var' => true,
          'rewrite' =>  array('slug'=>'type'),
          'hierarchical' => true,
          'show_admin_column' => true,
          'sort' => true,
          'public' => true
      );
      register_taxonomy( 'lgib_type', 'lgib_vehicule', $args );
    }
    
    add_action('init', 'lgib_vehicule_taxonomy');
    
    // ========================================================================
    // ------ custom meta-boxes associé au  custom-post-type véhicule ---------
    // ========================================================================
    function lgib_vehicule_meta_box()  {
        add_meta_box(
            'lgib_vehicule_meta',
            'Détails du véhicule',
            'lgib_vehicule_meta_populate',
            'lgib_vehicule',
            'advanced',
            'default'
        );
    }
    
    // =================================  fonction de remplissage des champs
    function lgib_vehicule_meta_populate($post)  {
        wp_nonce_field( plugin_basename(__FILE__), 'lgib_custom_type_noncename' );
    
        $serie = get_post_meta( $post->ID, 'lgib_vehicule_serie', true );   ?>
        <p>
        <label for="lgib_vehicule_serie">Série: </label>
        <input type="text" class="all-options" name="lgib_vehicule_serie" id="lgib_vehicule_serie"
        value="<?php echo esc_attr( $serie );   ?>"  />
        <span class="description">Entrer la série modèle du véhicule</span>
        </p>
    <?php
    }
    
    add_action('add_meta_boxes', 'lgib_vehicule_meta_box');
    
    // =================================  fonction de sauvegarde des champs
    function lgib_vehicule_meta_save($post_id)  {
        if ( defined( 'DOING_AUTOSAVE' )  && DOING_AUTOSAVE )  {
            return $post_id;
        }
    
        if ( !isset( $_POST['lgib_custom_type_noncename'] ) || !wp_verify_nonce( $_POST['lgib_custom_type_noncename'], plugin_basename(__FILE__) ) ) {
            return $post_id;
        }
    
        global $post;
        $post_type = get_post_type_object($post->post_type);
    
        if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) ) {
            return $post_id;  echo '03'; die();
        }
        //echo '02'; die();
        $metadata['lgib_vehicule_serie'] = ( isset ($_POST['lgib_vehicule_serie'] ) ? $_POST['lgib_vehicule_serie'] : '' ); 
    
          foreach ( $metadata as $key=>$value ) {
    
            $current_value = get_post_meta( $post_id, $key, true);
            if ( $value && '' == $current_value )  {
                add_post_meta( $post_id, $key, $value, true);
            } else if ( $value && $value != $current_value )  {
                update_post_meta( $post_id, $key, $value);
            } else if ( '' == $value && $current_value )  {
                delete_post_meta($post_id, $key, $current_value );
            }
        }
    }
    
    add_action('save_post', 'lgib_vehicule_meta_save');
    ?>

    Try this website.. I have always used this to generate custom post type and works well..

    Thread Starter wp-lionel

    (@wp-lionel)

    Thanks Anshu but the problem is not with custom post types…

    the problem is with custom taxonomy in fact 🙂

    Thread Starter wp-lionel

    (@wp-lionel)

    I don’t need your generator site ..

    my custom type and taoxonomy WORKS !! the problem is with the interface of WordPress

    Thread Starter wp-lionel

    (@wp-lionel)

    I have fixed the problem..

    it was due to white lines after the closing ?> of the index.php file from the plugin…

    🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘none refresh when my code is a plugin’ is closed to new replies.