[HELP] Different themes for wordpress pages and main site

Discussion in 'PHP' started by ramerika, Sep 17, 2011.

  1. #1
    I'm creating a automated webinar plugin for wordpress and have hit a road block. What I'm trying to do is allow users to create webinars from the plugin and have their selections (bgcolor, title text, video, etc) displayed on a different theme layout than their main theme activated. However I've seemed to have hit a roadblock.

    My initial thought was to have users upload several page template phps and css stylesheets into their main themes folder and have those files get the details from the plugin but I'm being told by my current developer that it can't be done that way because wp_insert_post won't allow it. Not sure why that matters but that's what I'm being told and I can't seem to find a way to make it work. Any suggestions?
     
    ramerika, Sep 17, 2011 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #2
    I'd go the myspace/vbulletin usercp way and have a plugin that captures their preferences in fields and then loads their webinars with a cutdown template. Your plugin will then write those preferences into a <style> section in the head of the page.

    Its easy to ensure the webinars always use the cutdown option - wordpress does that easily. Your plugin just fills in the gaps. If no info is given then it can use defaults you've coded in to ensure the page gets fleshed out.
     
    sarahk, Sep 18, 2011 IP
  3. ramerika

    ramerika Well-Known Member

    Messages:
    947
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    #3
    Cutdown template?

    The plugin has a settings menu users will submit their webinar details including layout info. Remember this plugin is for distribution so I need a solution that will work for the masses not just on my wordpress install.
     
    ramerika, Sep 18, 2011 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #4
    Yep, that's what I was suggesting. If not a cutdown template then one with the user's styles placed in the header so that they overwrite the default css.

    Steps.

    1. user puts default background, images etc into their user profile for the plugin - either in the user profile section or on a page just for the plugin
    2. user uploads webinar
    3. user views webinar on page and the page has additional styles added to it with the settings from #1
     
    sarahk, Sep 18, 2011 IP
  5. ramerika

    ramerika Well-Known Member

    Messages:
    947
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    #5
    I found the Gys Themed Categories plugin that does what I want to do but it only applies to post categories not pages which is what my webinar plugin creates. Any idea on how I can change this to apply to pages since they aren't associated to categories or tags? I've posted the code for the plugin's only file below:

    <?php
    /*
    Plugin Name: GYS Themed Categories
    Plugin URI: http://get-your-stuff.com/
    Description: This plugin allows you to assign themes to each of your Wordpress categories.  To assign themes to your categories, just go to Manage->Categories and you'll see a dropdown of all available themes at the bottom of the form. Get more GYS Themes and Plugins at <a href="http://get-your-stuff.com/">Get-Your-Stuff.Com</a>.
    Author: Mike Lopez
    Version: 2.1
    Author URI: http://mikelopez.info/
    */
    if(!class_exists('GYSThemedCategories')){
    	class GYSThemedCategories{
    		function GYSThemedCategories(){
    			$this->BlogCharset=get_option('blog_charset');
    			$this->OptionName=strtoupper(get_class($this));
    			$this->Options=get_option($this->OptionName);
    		}
    		function GetOption(){
    			$options=func_get_args();
    			$option=$this->Options;
    			foreach($options AS $o){
    				$option=$option[$o];
    			}
    			return $option;
    		}
    		function SetOptions(){
    			$options=func_get_args();
    			for($i=0;$i<count($options);$i+=2){
    				$this->Options[$options[$i]]=$options[$i+1];
    			}
    			update_option($this->OptionName,$this->Options);
    		}
    
    		// hooks
    
    		// CATEGORY FORM PROCESSING
    			function EditCategoryForm(){
    				$themes=get_themes();
    				$template=$this->GetOption('CategoryThemes',$_GET['cat_ID']);
    				$options='<option value="">---</option>';
    				foreach($themes AS $theme){
    					$selected=$theme['Template']==$template?' selected="true" ':'';
    					$options.='<option value="'.$theme['Template'].'"'.$selected.'>'.__($theme['Name']).' '.$theme['Version'].'</option>';
    				}
    				$form=<<<STRING
    				<div id="GYSThemedCategories">
    					<h3>GYS Themed Categories</h3>
    					<table class="form-table">
    						<tbody>
    							<tr class="form-field">
    								<th valign="top" scope="row">Category Theme</th>
    								<td><select name="GYSThemedCategories">{$options}</select></td>
    							</tr>
    						</tbody>
    					</table>
    				</div>
    				<script type="text/javascript">
    					//<![CDATA[
    					function GYSThemedCategories(){
    						try{
    							var x=document.getElementById('GYSThemedCategories');
    							var p=x.parentNode;
    							var t=p.getElementsByTagName('p')[0];
    							p.insertBefore(x,t);
    						}catch(e){}
    					}
    					GYSThemedCategories();
    					//]]>
    				</script>
    STRING;
    				echo $form;
    			}
    
    			function SaveCategory($id){
    				if(isset($_POST['GYSThemedCategories'])){
    					$catthemes=$this->GetOption('CategoryThemes');
    					if($_POST['GYSThemedCategories']){
    						$catthemes[$id]=$_POST['GYSThemedCategories'];
    					}else{
    						unset($catthemes[$id]);
    					}
    					$this->SetOptions('CategoryThemes',$catthemes);
    				}
    			}
    
    		// TEMPLATE PROCESSING
    			function Template($template){
    				$pid=$cid=0;
    				$perms=get_option('permalink_structure');
    				if($perms){
    					// get current URL if permalinks are set
    					$s=empty($_SERVER['HTTPS'])?'':$_SERVER['HTTPS']=='on'?'s':'';
    					$protocol='http'.$s;
    					$port=$_SERVER['SERVER_PORT']=='80'?'':':'.$_SERVER['SERVER_PORT'];
    					$url=$protocol.'://'.$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
    					list($url)=explode('?',$url);
    					// get Post ID from URL
    					$pid=url_to_postid($url);
    					// get Category ID from URL
    					list($url)=explode('/page/',$url); // <- added for paging compatibility
    					$cid=get_category_by_path($url,false);
    					$cid=$cid->cat_ID;
    				}else{
    					// no permalinks so we simply check GET vars
    					$pid=$_GET['p'];
    					$cid=$_GET['cat'];
    				}
    				
    				if($pid){
    					// we're in a post page... so let's get the first category of this post
    					list($cat)=wp_get_post_categories($pid);
    				}elseif($cid){
    					// we're in a category page...
    					$cat=$cid;
    				}
    				
    				if($cat){
    					// we have our category ID now so let's get the theme for it...
    					$theme=$this->GetOption('CategoryThemes',$cat);
    					// change template if a theme is specified for this category
    					if($theme)$template=$theme;
    				}
    
    				$this->Theme=$template;
    				return $template;
    			}
    			
    			function Stylesheet(){
    				return $this->Theme;
    			}
    
    	}
    }
    if(class_exists('GYSThemedCategories') && !isset($GYSThemedCategories)){
    	$GYSThemedCategories=new GYSThemedCategories(__FILE__);
    }
    
    if(isset($GYSThemedCategories)){
    	add_action('edit_category_form',array(&$GYSThemedCategories,'EditCategoryForm'));
    	add_action('create_category',array(&$GYSThemedCategories,'SaveCategory'));
    	add_action('edit_category',array(&$GYSThemedCategories,'SaveCategory'));
    
    	add_filter('template',array(&$GYSThemedCategories,'Template'));
    	add_filter('stylesheet',array(&$GYSThemedCategories,'Stylesheet'));
    }
    ?>
    Code (markup):
     
    ramerika, Sep 18, 2011 IP
  6. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #6
    ASAIK all this does is let the user pick an existing template for their category. Normally the theme is dependent on the category or id.

    Pages already let you set a theme so there's no gain there.

    Your point of difference is that the user can set the theme dependent on their userid isn't it?

    So if my wp install had 3 users all creating pages with webinars then all 3 could have a different look and feel, right?

    Not quite sure why they are pages anyway, surely webinars belong in posts?
     
    sarahk, Sep 18, 2011 IP
  7. ramerika

    ramerika Well-Known Member

    Messages:
    947
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    #7
    Right but they'd be able to maintain same core theme for rest of site. How does wp let you set a theme because tried with no success?
     
    ramerika, Sep 18, 2011 IP
  8. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #8
    sarahk, Sep 18, 2011 IP
  9. ramerika

    ramerika Well-Known Member

    Messages:
    947
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    #9
    so when the user setups webinar details including layouts, etc...use the code from the link and it will load the associated theme for my plugin? So for example if user current theme is Twenty Ten Theme and they install my plugin and theme in their wp setup when they create a webinar using my plugin with all details will load my plugins theme for those webinar pages instead of their current theme. And visitors will still get the Twenty Ten Theme on the users homepage. Is that correct?
     
    ramerika, Sep 18, 2011 IP
  10. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #10
    Yes, that's right.

    You can either let the user manually select the theme (which is then modified by the interface) or you can force it by overriding whatever they select.
     
    sarahk, Sep 18, 2011 IP
  11. ramerika

    ramerika Well-Known Member

    Messages:
    947
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    #11
    Ok cool. Do I code it before or after wp_insert_post or does it matter?
     
    ramerika, Sep 18, 2011 IP
  12. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #12
    I'd make the change at the time the page is called - that way any future changes to your plugin will be applied.
     
    sarahk, Sep 18, 2011 IP
  13. ramerika

    ramerika Well-Known Member

    Messages:
    947
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    #13
    ok thanks Sarah you've been a big help ... spent all week trying to figure this out ... feeling stupid for not asking sooner lol but thanks again
     
    ramerika, Sep 18, 2011 IP
  14. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #14
    I never cease to be impressed at the ways that wordpress let you mess with it. I even requested a hook once because it was missing and it was there in the very next release.
     
    sarahk, Sep 18, 2011 IP
  15. ramerika

    ramerika Well-Known Member

    Messages:
    947
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    #15
    it didn't work ...

    function custom_load_twenty_ten_template() {
        return 'twentyten';
    }
    
    function custom_load_twenty_ten_stylesheet() {
        return 'twentyten';
    }
    
    add_filter( 'template', 'custom_load_twenty_ten_template' );
    add_filter( 'stylesheet', 'custom_load_twenty_ten_stylesheet' );
    
    Code (markup):

    Where would I place the code for wordpress to recognize it?
     
    Last edited: Sep 18, 2011
    ramerika, Sep 18, 2011 IP
  16. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #16
    In the main script of your plugin
     
    sarahk, Sep 18, 2011 IP
  17. ramerika

    ramerika Well-Known Member

    Messages:
    947
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    130
    #17
    oh ok cuz I was thinking I should put this on the script for the page displays
     
    ramerika, Sep 19, 2011 IP