dimanche 13 mars 2016

Wordpress plugin doesn't work with single-post_type.php

I am creating a glossary plugin, I have to start learning somewhere.

FILES:

It creates a custom post_type mpact_glossary (code below)

I have created a file single-mpact_glossary.php (code below)

ISSUE:

This was working perfectly without thefile single-mpact_glossary.php.

I added the file single-mpact_glossary.php and it didn't work. It threw a 404 error. Upon deleting the template it started working again.

Now with or without the file is_single() throws a 404 error. Clearly I changed something when adding and removing file single-mpact_glossary.php. But, I don't know what.

Refreshing permalinks does nothing.

QUESTIONS:

  1. How do I not only get rid of the 404?

  2. How do I get the template file to work?

Any and all help would be appreciated.

Thanks for your time and consideration,

Tim

PLUGIN:

<?php

/*
   Plugin Name: !M Glossary
   Plugin URI: 
   Description: Plugin to create a Glossary
   Author: !mpactMEDIA
   Author URI: 
   Version: .5
*/

mb_internal_encoding("UTF-8");
class Mpact_Glossary {


    // Constructor
    function __construct() {
        add_action( 'init', array( &$this, 'mpact_glossary_register_post_type' ) );
        add_action( 'wp_enqueue_scripts', array( &$this, 'mpact_glossary_enqueue_style' ) );
        add_action( 'admin_enqueue_scripts', array( &$this, 'mpact_glossary_enqueue_options_style' ) );
        add_action( 'admin_menu', array( &$this, 'mpact_glossary_add_meta_boxes' ) );
        add_action( 'save_post', array( &$this, 'mpact_glossary_meta_box_save' ), 1, 2 );
        //Adminmenu
        add_action('admin_init', 'register_and_build_fields');
        add_action('admin_menu', 'theme_options_page');

    }



    // PHP4 Constructor
    function Mpact_Glossary() {
        $this->__construct();
    }

    // load css into the website's front-end
    function mpact_glossary_enqueue_style() {
        wp_register_style( 'mpact-glossary-style',  plugin_dir_url( __FILE__ ) . 'assets/css/frontend.css' );
        wp_enqueue_style( 'mpact-glossary-style' ); 
    }

    // load css into the admin pages
    function mpact_glossary_enqueue_options_style() {
        wp_register_style( 'mpact-glossary-options-style',  plugin_dir_url( __FILE__ ) . 'assets/css/admin.css' );
        wp_enqueue_style( 'mpact-glossary-options-style' ); 
    }


    function mpact_glossary_register_post_type() {

        $options = get_option('theme_options');

        register_post_type( 'mpact_glossary',
            array(
                'labels' => array(
                    'name' => __( '!M Glossary' ),
                    'singular_name' => __( 'term' ),
                    'add_new' => __( 'Add New' ),
                    'add_new_item' => __( 'Add New Term' ),
                    'edit' => __( 'Edit' ),
                    'edit_item' => __( 'Edit Term' ),
                    'new_item' => __( 'New Term' ),
                    'view' => __( 'View Term' ),
                    'view_item' => __( 'View Term' ),
                    'search_items' => __( 'Search Term' ),
                    'not_found' => __( 'No Terms found' ),
                    'not_found_in_trash' => __( 'No Terms found in Trash' )
                ),
                'public' => true,
                'query_var' => true,
                'show_in_menu' => true,
                'show_ui' => true,
                'menu_icon' => 'dashicons-book-alt',
                'supports' => array( 'title' ),
                'rewrite' => array( 'slug' => $options['slug_url_setting'] ? get_post($options['slug_url_setting'])->post_name : 'glossary', 'with_front' => false )


            )
        );

    }

    /**
    * Calls all of the functions responsible for rendering each of the meta boxes
    * use in the theme.
    *
    * @since 1.1.0
    **/
    function mpact_glossary_add_meta_boxes() {
        $this->_mpact_glossary_definition_add_meta_box();
    }

    /**
    * Renders the meta box responsible for allowing the user to enter the
    * glossary term definition.
    *
    * @since 1.1.0
    **/
    function _mpact_glossary_definition_add_meta_box() {
        add_meta_box('mpact_glossary', __('!mpactGlossary Definition of Term', 'mpact_glossary'), array( &$this, '_mpact_glossary_definiton_meta_box' ), 'mpact_glossary', 'normal', 'high');
    }

    function _mpact_glossary_definiton_meta_box() {
        global $post;

        printf( '<input type="hidden" name="_mpact_glossary_nonce" value="%s" />', wp_create_nonce( plugin_basename(__FILE__) ) );

        printf( '<p><label for="%s">%s</label></p>', '_mpact_glossary_description', __('Enter the Term\'s Definition', 'mpact_glossary') );
        //Enable the Editor / TinyMCE
        $settings = array( 'media_buttons' => true );
        wp_editor( get_post_meta( $post->ID, '_mpact_glossary_term', true ), '_mpact_glossary_term', $settings );
    }

    function mpact_glossary_meta_box_save( $post_id, $post ) {

        $key = '_mpact_glossary_term';

        //  verify the nonce
        if ( !isset($_POST['_mpact_glossary_nonce']) || !wp_verify_nonce( $_POST['_mpact_glossary_nonce'], plugin_basename(__FILE__) ) )
            return;

        //  don't try to save the data under autosave, ajax, or future post.
        if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
        if ( defined('DOING_AJAX') && DOING_AJAX ) return;
        if ( defined('DOING_CRON') && DOING_CRON ) return;

        //  is the user allowed to edit the URL?
        if ( ! current_user_can( 'edit_posts' ) || $post->post_type != 'mpact_glossary' )
            return;

        $value = isset( $_POST[$key] ) ? $_POST[$key] : '';

        if ( $value ) {
            //  save/update
            $my_post = array();
            update_post_meta($post->ID, $key, $value);


        } else {
            //  delete if blank
            delete_post_meta($post->ID, $key);
        }

    }

};

function mpact_glossary_directory() {
    $html = '<div>';
    $args = array( 'post_type' => 'mpact_glossary',  'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => '-1');
    $loop = new WP_Query( $args );

    $current_initial_letter = null;
    $current_initial_letter2 = null;

    if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post();
        $title = get_the_title();

        if($title):
            $entry_initial_letter = strtoupper(mb_substr($title, 0, 1));
            $isFirstCharLetter = ctype_alpha($entry_initial_letter);
            $permalink = get_permalink();

            if($isFirstCharLetter == True) {
                if (is_null($current_initial_letter) || $current_initial_letter != $entry_initial_letter) {

                    $html .= '<a name="'.$entry_initial_letter.'"></a><h3 style="border-bottom:1px solid #EBEBEB; clear:both;"><span style="margin:0 0 10px 10px;">'.$entry_initial_letter.'</span> </h3>';
                    $current_initial_letter = $entry_initial_letter;
                }
                $html .= '<p style="float:left; width:205px;"><a href="'.$permalink.'">'.$title.'</a></p>';
            } else {
                $headline = true;
                $nhtml .= '<p style="float:left; width:205px;"><a href="'.$permalink.'">'.$title.'</a></p>';
            }
        endif;
    endwhile;
    endif;
    if ($headline) {
        $html .= '<a name="all"></a><h3 style="border-bottom:1px solid #EBEBEB; clear:both;"><span style="margin:0 0 10px 10px;">#</span> </h3>';
    }
    $html .= $nhtml;
    $html .= '</div>';
    return $html;

}

add_shortcode('mpact_glossary_directory', 'mpact_glossary_directory');


function mpact_glossary_navigation() {
    $html = '';
    $args = array( 'post_type' => 'mpact_glossary',  'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => '-1'  );
    $loop = new WP_Query( $args );
    $array = array();
    if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post();
        $str = get_the_title();
        $array[] = strtoupper(substr($str, 0, 1));
    endwhile;
    endif;

    $clean_array = array_unique($array);
    for($i = 65; $i < 91; $i++) {

        if(in_array(chr($i), $clean_array)) {
            $html .= '<span style="float:left;font-size:18px; margin-left:10px; display: inline;"><a href="#'.chr($i).'">'.chr($i).'</a></span>';

        } else {
            $html .= '<span style="float:left;font-size:18px; margin-left:10px; display: inline; color:#d3d3d3;">'.chr($i).'</span>';
        }
    }

    if(!preg_match("/^[A-Z]$/", $clean_array[0]) OR !preg_match("/^[A-Z]$/", end($clean_array))) {
        $html .= '<span style="float:left;font-size:18px; margin-left:10px; display: inline;"><a href="#all">#</a></span>';
    } else {
        $html .= '<span style="float:left;font-size:18px; margin-left:10px; display: inline; color:#d3d3d3;">#</span>';
    }
    $html .='<div style="clear:left;"></div>';
    return $html;
};

add_shortcode('mpact_glossary_navigation', 'mpact_glossary_navigation'); 


function mpact_term_filter($definition) {

    if( is_singular('mpact_glossary') && is_main_query() ) {

        $new_definition = get_post_meta( get_the_ID(), '_mpact_glossary_term' );
        if($new_definition):
            $definition .= wpautop($new_definition[0]);
        endif;

        $title = get_the_title();

        $term = '<h1 class="h4">' . $title . ' Definition:</h1>';

        $lower_title = strtolower($title);
        $firstcap_title = ucfirst($title);

        $content = $term . $definition;

        return $content;

    } else {

        $content = $definition;

        return $content;
    }
};
add_filter('the_content', 'mpact_term_filter');


$MyMpactGlossary = new Mpact_Glossary;

// Adminmenu

function build_options_page() { ?>
    <div id="theme-options-wrap">
        <div class="icon32" id="icon-tools"> <br /> </div>
        <form method="post" action="options.php" enctype="multipart/form-data">
            <?php settings_fields('theme_options'); ?>
            <?php do_settings_sections(__FILE__); ?>
            <p class="submit">
                <input name="Submit" type="submit" class="button-primary" value="<?php esc_attr_e('Save Changes'); ?>" />
            </p>
        </form>
        <p><hr></p>
        <p>* After editing the Slug the Permalinks Settings must be saved again.</p>
    </div>
<?php }

function register_and_build_fields() {
    register_setting('theme_options', 'theme_options', 'validate_setting');
    add_settings_section('general_settings', 'General Settings', 'settings_general', __FILE__);

    function settings_general() {}
    add_settings_field('slug', 'Mainpage:', 'slug_url_setting', __FILE__, 'general_settings');
}

function validate_setting($theme_options) {
    return $theme_options;
}

function slug_url_setting() {
    $options = get_option('theme_options');
    wp_dropdown_pages(array('name' => 'theme_options[slug_url_setting]', 'selected' => $options['slug_url_setting'] ));
}

function theme_options_page() { add_options_page('!M Glossary', 'Glossary', 'administrator', __FILE__, 'build_options_page');}
?>

FILE (single-pact_glossary.php):

$new_definition = get_post_meta( get_the_ID(), '_mpact_glossary_term' );

    if($new_definition):
        $definition .= wpautop($new_definition[0]);
    endif;

    $title = get_the_title();

    $term = '<h1 class="h4">' . $title . ' Definition:</h1>';

    echo $term . $definition;



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire