WordPress.org

Ready to get started?Download WordPress

Forums

Responsive Slider
[Feature request] Move the html output into a separate template (3 posts)

  1. frankobrillo
    Member
    Posted 1 year ago #

    It would be a great idea if you could move the html output into a separate template file. Html code is now hard coded into the plugin which makes it hard to customize. Besides that, any custom changes to the html would get lost when updating the plugin.

    Please let me know if you need some help with the coding.

    http://wordpress.org/extend/plugins/responsive-slider/

  2. Griden
    Member
    Plugin Author

    Posted 1 year ago #

    I don't think template overwriting works for plugins the way it is for parent-child themes. Am I missing something?

    The best I can do is make the output filterable via a filter hook.

  3. frankobrillo
    Member
    Posted 1 year ago #

    It really doesn't have to be too complicated. My suggestion would be the following.

    1) Create a new file within the plugin directory and name it e.g. 'responsive-slider-template.php'
    2) Move the almost entire content from the function 'responsive_slider' (around line 230) to that file.
    3) If a file exists with the same name within the users theme directory, include that file, otherwise, include the default template.

    Nice and easy. See below for suggested code changes.

    Suggested code changes:

    file: responsive-slider.php
    =======================

    […]

    /**
     * Output the slider.
     *
     * @since 0.1
     */
    function responsive_slider() {
    
    	$slides = new WP_Query( array( 'post_type' => 'slides', 'order' => 'ASC', 'orderby' => 'menu_order' ) );
    
    	$template = false;
    	$basename = "responsive-slider-template.php";
    	$basedir = array(
    		"theme" => get_template_directory(),
    		"plugin" => dirname(__FILE__),
    	);
    
    	foreach($basedir as $location => $path) :
    		if (file_exists($path."/".$basename)) :
    			$template = $path."/".$basename;
    			break;
    		endif;
    	endforeach;
    
    	// get the html code from the template
    	$html = "";
    	if ($template) :
    		ob_start();
    		include $template;
    		$html = ob_get_clean();
    	else :
    		new WP_Error('broke', __("No valid template file found for 'Responsive slider'"));
    	endif;
    
    	wp_reset_query();
    	return $html;
    }

    file: responsive-slider-template.php
    =======================

    <?php
    /*
     * Template file for "Responsive slider"
     */
    ?>
    
    <div class="responsive-slider flexslider">
    	<ul class="slides">
    		<?php
    		/*
    		 * Start the slides loop
    		 */
    		while ( $slides->have_posts() ) :
    			$slides->the_post();
    			global $post; ?>
    			<li>
    				<div id="slide-<?php the_ID(); ?>" class="slide">
    				<?php
    				/*
    				 * output the post thumbnail
    				 */
    				if ( has_post_thumbnail() ) :
    
    					// include the link
    					$has_link = get_post_meta( $post->ID, "_slide_link_url", true );
    					if ( $has_link ) : ?>
    					<a href="<?php echo get_post_meta( $post->ID, "_slide_link_url", true ); ?>" title="<?php the_title_attribute(); ?>">
    						<?php echo get_the_post_thumbnail( $post->ID, 'slide-thumbnail', array( 'class' => 'slide-thumbnail' ) ); ?>
    					</a>
    					<?php else : ?>
    						<?php echo get_the_post_thumbnail( $post->ID, 'slide-thumbnail', array( 'class' => 'slide-thumbnail' ) ); ?>
    					<?php endif; ?>
    				<?php endif; ?>
    
    				<h2 class="slide-title">
    					<a href="<?php echo get_post_meta( $post->ID, "_slide_link_url", true ); ?>" title="<?php the_title_attribute (); ?>">
    						<?php the_title(); ?>
    					</a>
    				</h2>
    			</div><!-- #slide-x -->
    		</li>
    		<?php endwhile; // loop ends here ?>
    	</ul>
    </div><!-- #featured-content -->

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.