• dgcov

    (@dgcov)


    I’ve been studying the plugin tutorial for creating a image slider to familiarise myself with the WordPress plugin API.

    The tutorial is here.

    My (complete) plugin’s php file is as follows:

    <?php
    /*
        Plugin Name: Nivo Plugin
        Description: Simple implementation of a nivo slideshow into WordPress
        Author: Ciprian Turcu
        Version: 1.0
    */
    function np_init() {
        add_shortcode('np-shortcode', 'np_function');
        add_image_size('np_widget', 180, 100, true);
        add_image_size('np_function', 600, 280, true);
        $args = array(
            'public' => true,
            'label' => 'Nivo Images',
            'supports' => array(
                'title',
                'thumbnail'
            )
        );
        register_post_type('np_images', $args);
    }
    add_theme_support( 'post-thumbnails' );
    add_action('init', 'np_init');
    add_action('wp_print_scripts', 'np_register_scripts');
    add_action('wp_print_styles', 'np_register_styles');
    
    function np_register_scripts() {
        if (!is_admin()) {
            // register
            wp_register_script('np_nivo-script', plugins_url('nivo-slider/jquery.nivo.slider.js', __FILE__), array( 'jquery' ));
            wp_register_script('np_script', plugins_url('script.js', __FILE__));
    
            // enqueue
            wp_enqueue_script('np_nivo-script');
            wp_enqueue_script('np_script');
        }
    }
    
    function np_register_styles() {
        // register
        wp_register_style('np_styles', plugins_url('nivo-slider/nivo-slider.css', __FILE__));
        wp_register_style('np_styles_theme', plugins_url('nivo-slider/themes/default/default.css', __FILE__));
    
        // enqueue
        wp_enqueue_style('np_styles');
        wp_enqueue_style('np_styles_theme');
    }
    
    function np_function($type='np_function') {
        $args = array(
            'post_type' => 'np_images',
            'posts_per_page' => 5
        );
        $result = '<div class="slider-wrapper theme-default">';
        $result .= '<div id="slider" class="nivoSlider">';
    
        //the loop
        $loop = new WP_Query($args);
        while ($loop->have_posts()) {
            $loop->the_post();
            $the_url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), $type);
            $result .='<img title="'.get_the_title().'" src="' . $the_url[0] . '" data-thumb="' . $the_url[0] . '" alt=""/>';
        }
        $result .= '</div>';
        $result .='<div id = "htmlcaption" class = "nivo-html-caption">';
        $result .='<strong>This</strong> is an example of a <em>HTML</em> caption with <a href = "#">a link</a>.';
        $result .='</div>';
        $result .='</div>';
        return $result;
    }
    
    function np_widgets_init() {
        register_widget('np_Widget');
    }
    
    add_action('widgets_init', 'np_widgets_init');
    
    class np_Widget extends WP_Widget {
    
        public function __construct() {
            parent::__construct('np_Widget', 'Nivo Slideshow', array('description' => __('A Nivo Slideshow Widget', 'text_domain')));
        }
    }
    
    function form($instance) {
        if (isset($instance['title'])) {
            $title = $instance['title'];
        }
        else {
            $title = __('Widget Slideshow', 'text_domain');
        }
        echo '<p><label for="'.$this->get_field_id('title').'">';
        echo 'Title:</label><input class="widefat" id="'.$this->get_field_id('title').'" ';
        echo 'name="'.$this->get_field_name('title').'" type="text"';
        echo 'value="'.esc_attr($title).'" /></p>';
    }
    
    function update($new_instance, $old_instance) {
        $instance = array();
        $instance['title'] = strip_tags($new_instance['title']);
    
        return $instance;
    }
    
    function widget($args, $instance) {
        extract($args);
        // the title
        $title = apply_filters('widget_title', $instance['title']);
        echo $before_widget;
        if (!empty($title))
            echo $before_title . $title . $after_title;
        echo np_function('np_widget');
        echo $after_widget;
    }
    ?>

    It seems to work, however, there is no mechanism for uploading images or for attaching the URLs for images in the custom post type.

    What am I missing?

Viewing 3 replies - 1 through 3 (of 3 total)
  • domokun

    (@domokun)

    I added your plugin code and can see the image option.

    Suggest it might be a conflict with your theme?

    Try adding add_theme_support( ‘post-thumbnails’ ); to your functions.php instead

    Thread Starter dgcov

    (@dgcov)

    Hi domokun,

    (thanks very much for your response and sorry for my tardy reply)

    I removed the add_theme_suppport(‘post-thumbnails’) from the index.php and moved it to my theme’s functions.php as you suggested, but there remains no mechanism for including an image in the post type.

    I added add_theme_suppport(‘post-thumbnails’) to the file at the bottom in the root namespace.

    domokun

    (@domokun)

    That should have worked.

    Suggest you alter the theme (temporarily) to use Twenty Eleven/Twelve

    See if your plugin works then, I think it will. If it doesnt, you’ll have to bug hunt your new theme (or other active plugins) to see what’s preventing your code from showing, because it should and does (on my setup)

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Adding images to custom post type.’ is closed to new replies.