WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] [Plugin: Classy wp_list_pages] Does not work with WPMU 2.8+ (5 posts)

  1. simplistik
    Member
    Posted 4 years ago #

    We've been using this plug-in for a while. But we've just recently found out that this plug-in does not work with WPMU. When you try to update it goes to an error page that says: "Error! Options page not found."

    So I reworked the code to work w/ WP & WPMU to follow the 2.8 method of registering options, based off this article: http://planetozh.com/blog/2009/05/handling-plugins-options-in-wordpress-28-with-register_setting/

    This has no legacy support cause I was to lazy to write a few if statements. Just replace these lines with the new content:

    line 32 - 37

    add_action('admin_init', 'classy_wp_list_pages_init' );
    function classy_wp_list_pages_init() {
    	global $c_wp_lp_options;
    
    	if( function_exists( 'register_setting' ) ) {
    
    		foreach ( $c_wp_lp_options as $k => $v ) {
    			register_setting( 'classy-wp-list-pages' , $k );
    		}
    
    	}
    }

    line 194 - 196 (the stuff after the form tag)

    <?php settings_fields('classy-wp-list-pages'); ?>

    that's it. hope this helps other people as this is a very useful plug-in.

    the whole thing looks like:

    <?php
    /*
    Plugin Name: Classy wp_list_pages
    Plugin URI: http://code.dunae.ca/classy_wp_list_pages
    Description: Adds a uniqe class or ID attribute to each LI tag generated by wp_list_pages() allowing them to be individually styled with CSS.
    Author: Alex Dunae
    Version: 1.3.1
    Author URI: http://dunae.ca/
    */
    
    /**************************************
    
     Hooks and configuration
    
    ***************************************/
    /*add_filter('page_css_class', 'abcde', 10, 2);
    function abcde ($css_class, $page) {
    var_dump($css_class);
    var_dump($page);
    die;
    }*/
    add_filter('wp_list_pages','c_wp_lp_filter');
    add_action('admin_menu', 'c_wp_lp_add_menus');
    
    $c_wp_lp_options = array('c_wp_lp_prefix' => 'Class prefix',
                             'c_wp_lp_sep' => 'Class separator',
                             'c_wp_lp_attr' => 'Attribute',
                             'c_wp_lp_first_class' => 'Class to add to first element',
                             'c_wp_lp_last_class' => 'Class to add to last element');
    
    // for WordPress 2.7 and MU
    add_action('admin_init', 'classy_wp_list_pages_init' );
    function classy_wp_list_pages_init() {
    	global $c_wp_lp_options;
    
    	if( function_exists( 'register_setting' ) ) {
    
    		foreach ( $c_wp_lp_options as $k => $v ) {
    			register_setting( 'classy-wp-list-pages' , $k );
    		}
    
    	}
    }
    
    $c_wp_lp_values = array();
    
    /**************************************
    
     wp_list_pages() filter
    
    ***************************************/
    function c_wp_lp_filter_callback($matches) {
    	global $c_wp_lp_values;
    
    	$prefix = (strlen($c_wp_lp_values['c_wp_lp_prefix'])) > 0 ?
    	              $c_wp_lp_values['c_wp_lp_prefix'] . $c_wp_lp_values['c_wp_lp_sep'] :
    	              '';
    
    	// build the identifier
    	//  - strip trailing and preceding slashes
    	//  - replace the remaining slashes with the separator (from get_options())
    	$identifier = '';
    
    	if( $matches[4] && !empty($matches[4]) ) {
    		$identifier = preg_replace('/(^\/|\/$)/', '', $matches[4]);
    		$identifier = str_replace('/', $c_wp_lp_values['c_wp_lp_sep'], $identifier);
    	} else { // if the url is blank, this is the homepage
    		$identifier = 'frontpage';
    	}
    
    	$identifier = $prefix . $identifier;
    
    	$filtered = '';
    
    	if( $c_wp_lp_values['c_wp_lp_attr'] == 'id' ) {
    		$filtered = sprintf("<li id=\"%s\"%s><a href=\"%s\"", $identifier, $matches[1], get_option('home') . '/' . $matches[4]);
    	} else {
    		// append any existing classes and trim out extra spaces
    		$class = $identifier . ' ' . trim($matches[2]);
    		$class = preg_replace('/[ ]+/', ' ', $class);
    		$filtered = sprintf("<li class=\"%s\"><a href=\"%s\"", $class, get_option('home') . '/' . $matches[4]);
    	}
    
    	return $filtered;
    }
    
    function c_wp_lp_filter($content) {
    	global $c_wp_lp_values;
    
    	// load plugin options from the database
    	$c_wp_lp_values = c_wp_lp_get_values();
    
    	// escape the blog's base URL
    	$url = preg_replace(array('/\//', '/\./', '/\-/'), array('\/', '\.', '\-'), get_option('home'));
    
    	$pattern = '/<li( class=\"([\w\s_\-]+)\")?><a href=\"' . $url . '(\/([\w\-_\/]+))?"/i';
    
    	$content = preg_replace_callback($pattern, "c_wp_lp_filter_callback", $content);
    
    	// add class to first list item
    	if ( !empty($c_wp_lp_values['c_wp_lp_first_class']) ) {
    		$content = preg_replace('/(<ul>|^)[\s]*<li([\s]*id=("[\w_-]+"|\'[\w_-]+\')[\s]*)? class="/i',
    		                        "$1<li$2 class=\"" . $c_wp_lp_values['c_wp_lp_first_class'] . ' ',
    		                        $content,
    		                        -1);
    	}
    
    	// add class to last list item; reverses and tokenizes the string
    	if ( !empty($c_wp_lp_values['c_wp_lp_last_class']) ) {
    		$rev_class = strrev($c_wp_lp_values['c_wp_lp_last_class']);
    
    		// add spaces between tags and reverse
    		$content = strrev( preg_replace( '/><(\/?(ul|li))/i', '> <$1', $content ) );
    		$out = '';
    		$list_depth = 0;
    
    		$t = strtok( $content, " \n\t" );
    
    		while ($t !== false) {
    			if ( $list_depth > 0) {
    				// detected <code>page_item</code> class in the first LI before the UL
    				if ( $t == 'meti_egap'  ) {
    					$t .= " $rev_class";
    					$list_depth -= 1;
    				}
    			}
    
    			// increase depth after encountering a closing UL
    			if ( strcasecmp( $t, '>lu/<' ) == 0 ) {
    				$list_depth += 1;
    				$t .= "\n";
    			}
    
    			$out .= $t . ' ';
    			$t = strtok( " \n\t" );
    		}
    
    		$out = str_replace( '<li', "\n<li", strrev( $out ) );
    
    		$content = $out;
    	}
    
    	return $content;
    }
    
    /**************************************
    
     Menu functions
    
    ***************************************/
    function c_wp_lp_add_menus() {
    	add_options_page('Classy wp_list_pages Options', 'Classy wp_list_pages', 8, 'c_wp_lp_options', 'c_wp_lp_options_page');
    }
    
    // read this plugin's options from the database
    function c_wp_lp_get_values() {
    	global $c_wp_lp_options;
    
    	$opt_values = array();
    
    	foreach( $c_wp_lp_options as $k => $v )
    		$opt_values[$k] = get_option($k);
    
    	// default value for separator is an underscore
    	if(strlen(trim($opt_values['c_wp_lp_sep'])) < 1)
    		$opt_values['c_wp_lp_sep'] = '_';
    
    	// default value for attribute is ID
    	if(strlen(trim($opt_values['c_wp_lp_attr'])) < 1)
    		$opt_values['c_wp_lp_attr'] = 'id';
    
    	return $opt_values;
    }
    
    function c_wp_lp_options_page() {
    	global $c_wp_lp_options;
    
        echo '<div class="wrap">';
    
        echo "<h2>" . __( 'Classy wp_list_pages Options', 'c_wp_lp_trans_domain' ) . "</h2>";
    
        // options form
    
        ?>
    
    <form method="post" action="options.php">
    	<?php settings_fields('classy-wp-list-pages'); ?>
    
    	<p>Should the identifier be applied to the class or ID attribute?<br /><br />
    		<?php _e("Attribute:", 'c_wp_lp_trans_domain' ); ?>
    		<select name="c_wp_lp_attr">
    			<option value="id"<?php echo get_option('c_wp_lp_attr') == 'id' ? ' selected="selected"' : ''; ?>>ID</option>
    			<option value="class"<?php echo get_option('c_wp_lp_attr') == 'class' ? ' selected="selected"' : ''; ?>>Class</option>
    		</select>
    	</p><hr />
    
    	<p>An optional string appended to the beginning of the generated identifier (letters and numbers only).<br /><br />
    	<?php _e("Prefix:", 'c_wp_lp_trans_domain' ); ?>
    		<input type="text" name="c_wp_lp_prefix" value="<?php echo get_option('c_wp_lp_prefix'); ?>" size="5">
    	</p><hr />
    
    	<p>What character should separate each token in the identifier?<br /><br />
    	<?php _e("Token separator:", 'c_wp_lp_trans_domain' ); ?>
    		<select name="c_wp_lp_sep">
    			<option value="-"<?php echo $opt_values['c_wp_lp_sep'] == '-' ? ' selected="selected"' : ''; ?>>Dash (-)</option>
    			<option value="_"<?php echo $opt_values['c_wp_lp_sep'] == '_' ? ' selected="selected"' : ''; ?>>Underscore (_)</option>
    		</select>
    	</p><hr />
    
    	<p>	<?php _e("Class to add to the first list item:", 'c_wp_lp_trans_domain' ); ?>
    		<input type="text" name="c_wp_lp_first_class" value="<?php echo get_option('c_wp_lp_first_class'); ?>" size="25">
    	</p><hr />
    
    	<p>	<?php _e("Class to add to the last list item:", 'c_wp_lp_trans_domain' ); ?>
    		<input type="text" name="c_wp_lp_last_class" value="<?php echo get_option('c_wp_lp_last_class'); ?>" size="25">
    	</p><hr />
    
    	<p class="submit">
    		<input type="submit" name="Submit" value="<?php _e('Update Options ยป', 'c_wp_lp_trans_domain' ) ?>" />
    	</p>
    </form>
    </div>
    
    <?php } ?>
  2. Alex Dunae
    Member
    Posted 4 years ago #

    Thanks simplistik -- I'll work your fixes into the next release.

    Cheers,

    Alex

  3. Alex Dunae
    Member
    Posted 4 years ago #

    1.3.2 will be coming out in a few minutes with your fixes; thanks for taking the time to submit code -- makes things nice and easy for me!

  4. simplistik
    Member
    Posted 4 years ago #

    Not a problem I like to help where I can :)

  5. livelearncreate
    Member
    Posted 4 years ago #

    I'm using this plugin with wordpress MU 2.8.6 and the last item class is working?

    Any updates? Ideas on how to fix?

    Thanks in advance.

Topic Closed

This topic has been closed to new replies.

About this Topic