Page Style Switching plugin 90% done
-
Question for WordPress Experts, I am a coder but not a php coder, but through google and forum post I have 90% completed my first Plugin, not a ‘Hello World’, which is not bad progress.
My plugin will allow for the selection of a different theme via the admin panel, for any page on the blog, themes are selected from a dropdown where the user can choose a different style per page, then what my code does is loads the css from the themes folder.
After a couple of agitatated days it is all working here:
The only bug I have is that when I am in the Admin and I go to Pages > edit, it hides all the pages except one , when I disable the plugin all pages appear.
So it looks like the code is setting a filter any ideas on how to release the filter after the stylesheet is loaded?
/* This is called from the header.php */ /* returns a style sheet if one is selected for that page */ function drf_stylesheet() { $pagename = curPageName(); $pageid = str_replace(" ","_",$pagename); if (get_option("drf_theme_".$pageid)!='Default') { echo '<link rel="stylesheet" href="'; echo drf_get_themes_url().'/'.get_option("drf_theme_".$pageid).'/style.css" type="text/css" media="screen" />'."\n"; } else { return; } } /* This function returns the current page name */ function curPageName() { global $post; return $post->post_title; }
Regards
David (email: [email protected])
-
What is this hooked to? I’m wondering because off the top of my head I don’t see any reason to run the filter in the admin section at all. If you don’t run it in the admin section it may not mess with the Pages page. But here is a problem you are going to have and it may be related.
The page templates that appear on the right sidebar on the Pages edit page are culled from the active template. What you’ve done must be impacting that somehow and is likely to cause trouble if you’ve not compensated. Of course, I don’t know the whole of what you’ve done so I’m speculating really. The query that pulls up the list of pages to populate the table may also be in some way dependent upon the active theme. Again, I’m speculating. I don’t know what you’ve done, I’ve never tried to do anything similar, and I haven’t poked around in the core with this in mind.
Hi, thanks for your reply, it is working fine when you visit the website, and in the code I have just added it to the themes menu section, that should not upset the Pages > Edit,
add_theme_page( __("Manage Pagestyles", 'drfss'), __("Page Styles", 'drfss'), 'edit_themes', basename(__FILE__), "drf_ss_admin");
The hook I created:
register_activation_hook(__FILE__,'drf_ss_install');
This is called before loading the admin page:
if (is_admin()) { //Include menus require_once(dirname(__FILE__).'/adminmenu.php');
Any ideas?
David
Hi,
I just tried moving it from themes to general options and still the pages are filtered out, even when I go straight to admin > pages > edit, so there must be a conflict somewhere, but I do not know how to debug it!David ??
Getting there, breaking it down function by function, I remarked out the include admin page and the style switcher and the pages all displayed ok, so I know the issue comes up when the admin page is loaded, all the pagenames are added to an array for a dropdown, there is this line:
$pages = get_pages(); foreach ($pages as $pagg) { loads the array here }
Looking at the help for get_pages() I cannot see anything to suggest that this would cause a problem.
David ??
Can anyone think what might be happening, I am so close to finishing this plugin, or could you suggest some working code to get all page names into an array without using get_pages()maybe a database query, so I could then eliminate the get_pages() as the problem.
I am a coder but not confident with php but learning fast, and I would not post here until I had exhausted the Google searches, to the point of pulling out all my hair.
Regards
David ??I have tried different themes so the conflict is not there, when I remark out the get pages code i get the problem, I have also tried a database query to get the pages still the same problem, so I am very confused.
Remark out this block of code and all is well, enable it and you cannot view the Pages > Edit says ‘No Pages’
/* add each page to an array for the Style Switcher setup */ if (!$blogpages) $blogpages = get_pages('sort_column=menu_order'); foreach ($blogpages as $pagg) { $pagename = $pagg->post_title; $pageref = str_replace(" ","_",$pagename); $pagetheme = "drf_theme_".$pageref; $pagename." Page"; array_push($themeoptions, array( "name" => $pagename, "type" => "heading")); array_push($themeoptions, array( "name" => "Theme", "id" => $pagetheme, "type" => "select", "options" => $themes, "std" => "Default")); }
Here is all the code for the admin screen:
<?php /* Prevent direct access to this file */ if (!defined('ABSPATH')) { exit(__( "Sorry, you are not allowed to access this file directly.",'Digital Raindrops')); } /* This section creates the admin page Style Swapper area */ $themename = get_current_theme(); $themehortname = 'drf_'; $thisplugin->plug_domain; $themes = drf_get_theme_list(); /* create an array for the admin screen page options */ $themeoptions = array ( array("name" => "ADD THEMES STYLESHEETS TO INDIVDUAL PAGES", "type" => "heading", "desc" => "You can choose a different theme Stylesheet for any page<br /><br?> This will not change the theme just the Stylesheet")); /* add each page to an array for the Style Switcher setup */ if (!$blogpages) $blogpages = get_pages('sort_column=menu_order'); foreach ($blogpages as $pagg) { $pagename = $pagg->post_title; $pageref = str_replace(" ","_",$pagename); $pagetheme = "drf_theme_".$pageref; $pagename." Page"; array_push($themeoptions, array( "name" => $pagename, "type" => "heading")); array_push($themeoptions, array( "name" => "Theme", "id" => $pagetheme, "type" => "select", "options" => $themes, "std" => "Default")); } /* Get a list of Installed Themes */ function drf_get_theme_list() { $dirname = get_theme_root().'/'; $theme[] = "Default"; foreach(scandir($dirname) as $item) { if (is_dir($item)) { continue; } if (!is_file($item)) { $theme[] = $item; } } return $theme; } function drf_ss_update_option($key, $value){ update_option($key, (get_magic_quotes_gpc()) ? stripslashes($value) : $value); } function drf_ss_add_admin() { global $themename, $themeshortname, $themeoptions, $thisplugin; if ( $_GET['page'] == basename(__FILE__)) { if ('save' == $_REQUEST['action'] ) { foreach ($themeoptions as $value) { if($value['type'] != 'multicheck'){ drf_ss_update_option( $value['id'], $_REQUEST[ $value['id'] ] ); }else{ foreach($value['options'] as $mc_key => $mc_value){ $up_opt = $value['id'].'_'.$mc_key; drf_ss_update_option($up_opt, $_REQUEST[$up_opt] ); } } } foreach ($themeoptions as $value) { if($value['type'] != 'multicheck'){ if( isset( $_REQUEST[ $value['id'] ] ) ) { drf_ss_update_option( $value['id'], $_REQUEST[ $value['id'] ] ); } else { delete_option( $value['id'] ); } }else{ foreach($value['options'] as $mc_key => $mc_value){ $up_opt = $value['id'].'_'.$mc_key; if( isset( $_REQUEST[ $up_opt ] ) ) { drf_ss_update_option( $up_opt, $_REQUEST[ $up_opt ] ); } else { delete_option( $up_opt ); } } } } header("Location: options-general.php?page=adminmenu.php&saved=true"); die; } } add_options_page( __("Manage Pagestyles", 'drfss'), __("Page Styles", 'drfss'), MANAGEMENT_PERMISSION, basename(__FILE__), "drf_ss_admin"); } function drf_ss_admin() { global $themename, $themeshortname, $themeoptions, $thisplugin; if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"><p><strong>settings saved.</strong></p></div>'; ?> <div class="wrap"> <h2>Page Stylesheets</h2> <form method="post"> <table class="optiontable" style="width:100%;"> <?php foreach ($themeoptions as $value) { switch ( $value['type'] ) { case 'text': drf_ss_option_wrapper_header($value); ?> <input style="width:35%;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>" type="<?php echo $value['type']; ?>" value="<?php if ( get_settings( $value['id'] ) != "") { echo get_settings( $value['id'] ); } else { echo $value['std']; } ?>" /> <?php drf_ss_option_wrapper_footer($value); break; case 'select': drf_ss_option_wrapper_header($value); ?> <select style="width:40%;" name="<?php echo $value['id']; ?>" id="<?php echo $value['id']; ?>"> <?php foreach ($value['options'] as $option) { ?> <option<?php if ( get_settings( $value['id'] ) == $option) { echo ' selected="selected"'; } elseif ($option == $value['std']) { echo ' selected="selected"'; } ?>><?php echo $option; ?></option> <?php } ?> </select> <?php drf_ss_option_wrapper_footer($value); break; case "heading": ?> <tr valign="top"> <td colspan="2" style="text-align: left;"><h3><?php echo $value['name']; ?></h3></td> </tr> <?php break; default: break; } } ?> </table> <p class="submit"> <input name="save" type="submit" value="Save changes" /> <input type="hidden" name="action" value="save" /> </p> </form> </div> <?php } function drf_ss_option_wrapper_header($values){ ?> <tr valign="top"> <th scope="row" style="width:1%;white-space: nowrap;"><?php echo $values['name']; ?>:</th> <td> <?php } function drf_ss_option_wrapper_footer($values){ ?> </td> </tr> <tr valign="top"> <td> </td><td><small><?php echo $values['desc']; ?></small></td> </tr> <?php } add_action('admin_menu', 'drf_ss_add_admin'); ?>
Any help is most welcomed.
David ??
Getting pages via MySql is pretty simple. Query the wp_posts table for all post where post_type = ‘page’. That is a pretty raw result though, you won’t be getting attachments or meta data, but I guess you worked that out.
What is going on here?
$pagetheme = "drf_theme_".$pageref; $pagename." Page";
Look at the second line. Shouldn’t that be
$pagename = $pagename." Page";
?@ apljd,
Hi the Page Name is just a label the period ‘.’ after a string as you know is a concatenate for the ‘Contact Us’ page we do not need the ‘=’ as the $pagename was assigned with the first call so can just use.$pagename = $pagg->post_title; $mystring = 'This is the '; $mystring. $pagename; $mystring. ' Page';
This will return ‘This is the Contact Us Page’ I am happy that bit of code is working as expected, I also did test a database query but it was still the same problem, different themes gives the same result, so it leaves the array and the page rendering to look at!
/* Get the Page Name Contact Us */ $pagename = $pagg->post_title; /* Replace the space for an underscore for saving options Contact_Us */ $pageref = str_replace(" ","_",$pagename); /* create the unique identifier = drf_theme_Contact_Us */ $pagetheme = "drf_theme_".$pageref; /* Create the title = Contact Us Page */ $pagename." Page"; /* Add this to the Array as a heading */ array_push($themeoptions, array( "name" => $pagename, "type" => "heading")); /* Add this to the Array for an add_option()or update_option() */ array_push($themeoptions, array( "name" => "Theme", "id" => $pagetheme, "type" => "select", "options" => $themes, "std" => "Default"));
Regards
David (“Still baffeled and looking for a solution”)
Hi the Page Name is just a label the period ‘.’ after a string as you know is a concatenate for the ‘Contact Us’ page we do not need the ‘=’ as the $pagename was assigned with the first call so can just use.
Except that what you just described doesn’t work. I just tested it to make sure. Try this:
<?php $test = 'hello'; echo $test; echo '<br />'; $test.' world'; echo $test; echo '<br />'; $test = $test.' world'; echo $test; ?>
I get:
hello hello hello world
Ooops!
Sorry I just looked again and you are quite correct!I will correct that thanks, I am still working on the main problem now.
David
I have resolved this by changing the string variable names, it was a problem with my naming of the variables the same as a WordPress global variable?
The WordPress Admin > Pages > Edit uses one of these variable names and I was re assigning it, my original code was based on this WP example here!:
$pages = get_pages(); foreach ($pages as $pagg) { $option = '<option value="'.get_page_link($pagg->ID).'">'; $option .= $pagg->post_title; $option .= '</option>'; echo $option;
Original code I had written used the $pages variable ??
/* add each page to an array for the Style Switcher setup */ $pages = get_pages(); foreach ($pages as $pagg) { $pagetitle = $pagg->post_title; $pagename = $pagetitle." Page"; array_push($themeoptions, array( "name" => $pagename, "type" => "heading")); }
Changed to this and it is working fine, so maybe the lesson for me is to name my variables with a pre fix to save any conflict!
/* add each page to an array for the Style Switcher setup */ $drfpages = get_pages(); foreach ($drfpages as $pagg) { $drfpagetitle = $pagg->post_title; $drfpagename = $drfpagetitle." Page"; array_push($themeoptions, array( "name" => $drfpagename, "type" => "heading")); }
Any comments and guidence is more than welcomed, I will also avoid using these same Global variable names in my code like ‘$pages‘.
David (24 hours of confusion but a lesson well learned)
Joomla!, I think it is, has this capability built into the core.
Are you intending to release this? I’d kinda like to see to whole thing. Honestly, it strikes me as a bit weird to assign different templates to different pages, but I am curious about the code and it might be useful for making, say, a holiday themed page or something.
Hi apljdi,
I am using WordPress for about a year but never got under the bonnet to look at the engine, I starting creating themes with Artisteer which is great for creating clean themes quickly but only with two fixed sidebars, so I started to play with the themes.One question was coming up often from users that were ok at creating themes but not changing them, all this plugin does is load a second css file.
You may have a different background or header, or have different block headers on a page, so you would strip down the second theme to a few lines and upload this and any images, when the page loads it will load the second css changing the original look.
The content will not change just the look of the page, I have it working here Style Switcher it will be released as a Plugin but I have to tidy it up first, when the theme or plugin is deactivated remove the options settings etc:
Also I need to look at how to release my first Plugin!
I have created a few themes with artisteer and modifed some, they can be found on my website here, anyone can contact me from there if they have a local setup and want a copy to evaluate, to give me much needed feedback and suggestions on how to do it better.
David
- The topic ‘Page Style Switching plugin 90% done’ is closed to new replies.