I have an idea for running multiple wordpress themes at ounce. This might be a good thing to build into a wordpress plugin, if possible. And yes, I might undertake such a task if it isn't ridiculously hard.
(Also, if you are interested in teaming up with me, let me know (leave a comment), I am decent at javascript and php, but not to great, and would love some help!)
This is how I see it working:
Current "set" theme is accessible here: "www.foo.com/"
Second theme accessible here: "www.foo.com/index.php?set_theme=theme2&"
Third theme accessible here: "www.foo.com/index.php?set_theme=THEME_NAME_HERE&"
etc...
This could be used for javascript fall backs. For example, if you go to www.foo.com/?page_id=9 and have javascript turned on, you will hit a javascript redirect to "www.foo.com/index.php?set_theme=THEM_WITH_JAVASCRIPT&page_id=9".
This is how I imagine the plugin code looking/working:
if(isset($_GET['set_theme'])){
$loadme = cleaned($_GET['set_theme']);
if($loadme exists){
loadtheme($loadme);
} else {
//go on as usual, as if this plugin doesnt exist
}
} else {
//go on as usual, as if this plugin doesnt exist
}
And of course, all the links would have to ad ?set_theme=FOOBAR&
So, my main questions are:
How and where does Wordpress select the current theme?
How/where would you ad this to modify internal links-
if(isset($_GET['set_theme'])){
echo "?set_theme=" . $_GET['set_theme'];
}
Do you know of any good websites to point me in the right direction as to how to make WP plugins?
You might like to look at the Theme Switcher plugin to see how that accomplishes this task - should give you some ideas.
Don't really need a plugin, just add this to functions.php:
function theme_switcher() {
if(isset($_GET['theme']) $theme = $_GET['theme'];
global $wpdb;
if (isset($theme)) {
$wpdb->prefix
$queries = “UPDATE “.$wpdb->prefix.”options SET option_value = ‘”.$theme.”‘ WHERE option_name = ‘template’ OR option_name = ‘stylesheet’ OR option_name = ‘current_theme’;”;
$wpdb->query($query);
}
}
add_action('wp_head','switchTheme')
then use mywebsite.com/?theme=myNewTheme to switch them
Disclaimer: untested!!
Found a plugin that was 98% of the way there. Wordpress Theme Switcher Reloaded.
Just changed the ts_get_theme() function as follows:
function ts_get_theme() {
if (!empty($_GET["wptheme"])) {
return $_GET["wptheme"];
} else {
return '';
}
}
Related
I have worked on a few project with drupal, and the main suggestion I got when learning was to use Views to output your data, although this was fine for some items I found it cumbersome to achieve my projects requirements in the set time and to style it the controls were too limiting.
I want to know whats wrong with doing it like this, or whats right about doing it like this?
You run a query to get the specific data you want.
function custom_block_get_item($nid){
$result = db_query("SELECT `field_custom_item` FROM {field_data_field_custom_item} WHERE entity_id = :entity_id",
array(
':entity_id' => $nid,
));
return $result;
}
Then you loop through creating the html
function custom_block_display_blurb($nid){
$html='<div id="blurb_wrapper"><div id="recent_projects_blurb">';
$result=custom_block_get_blurb($nid);
while($row1 = $result->fetchAssoc()){
$a=check_markup($row1['field_custom_item'],'full_html','','');
$html.='
<span class="recent_project_text">'.
$a.'</span>';
}
$html.='</div></div>';
return $html;
}
Finally display it at the end
function custom_block_block_view($delta=''){
if (arg(0) == 'node' && is_numeric(arg(1))) $nodeid = arg(1);
switch($delta) {
case 'custom_block':
$block['content'] = custom_block_display_blurb($nodeid);
break;
}
return $block;
}
This seems like a kind of hacky way to do things, what is the correct drupal way?
Maybe you mean using the drupal theme layer? This allows for use of .tpl files and overriding. I have a simple block module that does it here Source here.
Here is some documentation on using the theme layer within a module.
Here is some official Drupal docs on themeing (mainly themes rather than using the theme layer in a module).
I have a WordPress site that has a standard Page called Places with URL
example.com/places/
And I have several child Pages called by cities
example.com/places/city-1
example.com/places/city-2
Now, I have a custom post type Place that should indicate a single place and should have permalink like
example.com/places/place-1
But then if I go to one of the previous links with city-1, city-2 I get 404 obviously because there is no place with that permalink.
Is there a way for WordPress to drop to previous permalink. So if there is no place with that name, look for a page with it.
You could probably use the the REFERER-value from the PHP server variable $_SERVER, but it´s not very reliable and can be altered.
I am using the plugin "Permalink Finder" in one of the pages I am maintaining and that works quite well for finding changed URL. You could give it a try and see if it works for you, too.
In case somebody ever having a similar problem, it can be done by using verbose page rules. This is an example I found at WordPress Stack Exchange
https://wordpress.stackexchange.com/questions/22438/how-to-make-pages-slug-have-priority-over-any-other-taxonomies-like-custom-post
add_action( 'init', 'wpse16902_init' );
function wpse16902_init() {
$GLOBALS['wp_rewrite']->use_verbose_page_rules = true;
}
add_filter( 'page_rewrite_rules', 'wpse16902_collect_page_rewrite_rules' );
function wpse16902_collect_page_rewrite_rules( $page_rewrite_rules )
{
$GLOBALS['wpse16902_page_rewrite_rules'] = $page_rewrite_rules;
return array();
}
add_filter( 'rewrite_rules_array', 'wspe16902_prepend_page_rewrite_rules' );
function wspe16902_prepend_page_rewrite_rules( $rewrite_rules )
{
return $GLOBALS['wpse16902_page_rewrite_rules'] + $rewrite_rules;
}
I know how to enable/disable comments on all pages. But how can I enable only a few, WITHOUT USING THE UI. In functions.php I just wanna go
if($this_is_a_page_i_want_comments_on) { enable_comments(); }
enable_comments(); doesn't exist, this is the part I need help with.
I could do this using WordPress Admin by allowing comments on all pages, then going to each page and disabling comments where I don't want them. This would take too long though.
P.S. I'm using Genesis framework.
Simply hook to the comments_open filter :) If you look-up in the source how the comments_open() function works, you'll notice that the function gets the post in question and then runs it's reposnse through the comments_open filter. Here's an example function to override that:
function my_override_comments_open( $open ) {
if ( $this_is_a_page_i_want_comments_on ) {
$open = true;
}
return $open;
}
add_filter('comments_open', 'my_override_comments_open', 1000);
I assume that you know how you'll identify the pages that you want to enable comments on - so that's up to you.
PP: I don't know if it will work in Genesis or not(I presume that it should though).
if (is_single('page_name_here') || is_single('other_page')) {
// Show the comment form here
}
Not sure if there's a better way, that's just my solution!
I was going to update the ecommerce plugin (Shopp) on my wordpress site and it asked me to deactivate it. once I did that I lost the entire site.
I am trying to activate the plugin through the php files, but not sure what I am doing and would like some help.
Does anyone know how I can activate the Shopp plugin (or any plugin for that matter) on my site through the php files?
This is the code I'm using to get the string:
$unserialized = unserialize('a:14:{i:0;s:19:"akismet/akismet.php";i:1;s:37:"breadcrumbs-plus/breadcrumbs-plus.php";i:2;s:35:"googleanalytics/googleanalytics.php";i:3;s:45:"grunion-contact-form/grunion-contact-form.php";i:4;s:43:"image-caption-links/image-caption-links.php";i:5;s:29:"image-widget/image-widget.php";i:6;s:13:"rate/rate.php";i:7;s:33:"restore-jquery/restore-jquery.php";i:8;s:41:"shopp-cache-helper/shopp-cache-helper.php";i:9;s:47:"shopp-default-breadcrumb-extender-sdbe/sdbe.php";i:10;s:33:"shopp-improved/shopp-improved.php";i:11;s:19:"shuffle/shuffle.php";i:12;s:19:"vslider/vslider.php";i:13;s:41:"wordpress-importer/wordpress-importer.php";}');
array_push($unserialized, 'shopp/shopp.php');
$serialized = serialize($unserialize);
echo $serialized;
The active plugins are not stored in a PHP file. It's stored in the database. Open the wp_options table in the database. Look for a row in which the value of the option_name field is active_plugins. In this row, look for the value of option_value. You'll see a serialized string containing the information of the active plugins.
Now, it might be a little bit confusing to edit the string straight away especially if you're not familiar how serialized strings are formatted. So, I suggest you copy the string and use PHP unserialize() function on it, which will then return an array. After that, use array_push() to add another element in which the value is the path to the plugins file (e.g. "akismet/akismet.php", in your case it might be "shopp/shopp.php"). Once you've add another element, use serialize() and copy the returned string and replace the old serialized string in the database.
$unserialized = unserialize('...');
array_push($unserialized, 'shopp/shopp.php');
$serialized = serialize($unserialized);
echo $serialized; // Copy this output back into the database
There are details on this site about how to programmatically activate and deactivate a plugin. Here is a snippet:
function toggle_plugin() {
// Full path to WordPress from the root
$wordpress_path = '/full/path/to/wordpress/';
// Absolute path to plugins dir
$plugin_path = $wordpress_path.'wp-content/plugins/';
// Absolute path to your specific plugin
$my_plugin = $plugin_path.'my_plugin/my_plugin.php';
// Check to see if plugin is already active
if(is_plugin_active($my_plugin)) {
// Deactivate plugin
// Note that deactivate_plugins() will also take an
// array of plugin paths as a parameter instead of
// just a single string.
deactivate_plugins($my_plugin);
}
else {
// Activate plugin
activate_plugin($my_plugin);
}
}
For everyone who has a plugin acting weird
The easiest way to regain access to your site when being locked out after deactivating, activating, installing, updating a plugin is:
Go to your webhost adminpanel (Cpanel, DirectAdmin)
Go to Files (Filemanager)
Go to //wp-content/ and rename your "plugins" folder to something else for instance "plugins_off"
Go to your WP-admin. You will have access again but no plugins visible.
Go back to your webhost adminpanel and rename "plugins_off" back to "plugins".
Now your plugins will all be listed again in WP-admin, but all deactivated.
Take it from there.
There is no need to add PHP code.
Like RST wrote: No need for coding to restore site that crashed after you deactivate a plugin. There's a good reason the site crashed because other plugins depended on that deactivated plugin (like WooCommerce).
The easiest way to restore the site and gain access to WP admin is simply to rename the plugins folder, refresh the page, go back and rename the folder back to "plugins" then hit refresh again. All plugins will be back as before only they are all deactivated. Now all you need to do is to activate them again. Do it one by one to be sure nothing else crashes.
Here is working code
(just uncomment "activate" line):
function MY_toggle_plugins() {
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
$temp_files1 = glob(WP_PLUGIN_DIR.'/*');
$activated=array();
$already_active=array();
foreach($temp_files1 as $file1){
if(is_dir($file1)) {
$temp_files2 = glob($file1 . '/*');
foreach($temp_files2 as $file2){
if(is_file($file2) && stripos(file_get_contents($file2),'Plugin Name:')!==false) {
$plugin_name_full=basename(dirname($file2)).'/'.basename($file2);
if(is_plugin_active($plugin_name_full)) {
array_push($already_active, $plugin_name_full);
//deactivate_plugins($plugin_name_full);
}
else{
array_push($activated, $plugin_name_full);
//activate_plugin($plugin_name_full);
}
}
}
}
}
echo 'You have activated these plugins:<br/><br/>'.serialize($activated).'<br/><br/>These were already active:<br/><br/>'.serialize($already_active); exit;
}
//execute
MY_toggle_plugins();
Ok, here's the deal: I am constructing a Drupal website that has several different sections. Each section is a view that displays a content type. (Each section has it's own content type) For example, I have a view that points to ?q=blog which displays content type blog.
All the sections look a little different than each other. Not like 'website-within-a-website' different but different enough that they can't all use the same template file and each be modified with CSS. Each section needs it's own page.tpl.php.
Unfortunately, AFAIK Drupal theme's .info files can only either assign one page.tpl.php for the entire theme or assign a page-node-####.tpl.php for each node. There is going to be lots of content on this website so setting Drupal to make a new identical page-node-####.tpl.php for every created node would get unmanagable very fast.
To solve this problem, I am going to use pathauto to create an alias for each content type. For example, all nodes of content type blog are given an alias ?q=blog/[post title]. Modify template.php to use page-blog.tpl.php for any page who's alias starts with the word 'blog'.
Other people have tried doing this sort of thing and have created functions such as the one described. Unfortunately, all the ones I have seen are for Drupal 6 or below. I have tried modifying existing ones with no success. So far, though, I think this is on the right track:
function basic_preprocess_page(&$vars, $hook) {
...
if( module_exists('path') ) {
$alias = drupal_get_path_alias( $_GET['q'] );
$site_section = "blog";
if( strpos( $alias, $site_section ) === 0 ) {
$VARIABLE_THAT_TELLS_THE_PAGE_WHAT_TEMPLATE_TO_USE = "/path/to/page-blog.php";
}
}
}
I cannot find $VARIABLE_THAT_TELLS_THE_PAGE_WHAT_TEMPLATE_TO_USE does anyone know what it is?
Maybe my site is structured badly. If anyone knows how to restructure my site so I can more easily make a theme with seperate sections please share how!
Thanks a million! (c:
EDIT: Perhaps I need to use template suggestions instead. Does anyone know the function or variable to use to set this?
They changed the name of this array key in D7 and I haven't seen it documented anywhere. I finally figured this out after a good bit of debugging. You can override the theme template in template.php with a hook_preprocess_page() like so:
function myTheme_preprocess_page(&$vars) {
global $node;
if ($node->type == 'blog') {
$vars['theme_hook_suggestions'] = array('my__blog_template'); // use my--blog-template.tpl.php, note '-' = '_'
}
elseif ($node->type == 'articles') {
$vars['theme_hook_suggestions'] = array('article__node_template'); // use article--node-template.tpl.php
}
}
Oh and don't forget to flush the Drupal caches after making changes to your template.php.
Ok, I found it:
http://drupal.org/node/223440#comment-991840
$alias = drupal_get_path_alias($_GET['q']);
if ($alias != $_GET['q']) {
$template_filename = 'page';
foreach (explode('/', $alias) as $path_part) {
$template_filename = $template_filename . '-' . $path_part;
$variables['template_files'][] = $template_filename;
}
}
Credit to this function goes to user mfb.
I had a lot of trouble with this so I will explain it here in case anyone finds it useful.
This function goes in your template.php. It needs to be part of the <theme name>_preprocess_page function. What it does is it takes the alias and then explodes it into a bunch of different components. For example if you are on a page with the alias ?q=blog/blog-post-title it would be exploded into blog and blog-post-title. It then turns each component into a name for a template suggestion. It puts each template suggestion into the template_files[] array (inside the $variables[] array) so that the page now has two new template suggestions:
page-blog, and page-blog-blog-post-title
Template suggestions are alternate template files. In this case they are for pages, but they don't necessarily have to be. You can have template suggestions for anything you can think of including blocks, nodes and the like. Don't let the name 'template suggestion' fool you. Template suggestions will be used over default templates as long as they exist. I don't know why it was named like that. I think it should be renamed.
What you do, then, now that you've set up Drupal to look for a template suggestion that points to your alias, is create a new template file where all the rest are in your theme. In this case, let's say I want to theme my entire blog section. In the templates folder I should create a file named page--blog.tpl.php (note the --double hyphens--) with the layout I want.
Drupal will use the most specific template suggestion it can find so if you wanted you could make one blog post to look completely different than the rest of the site long as you make a template for it named page--blog--blog-post-title and put it in your theme's templates directory. (again, note the double hyphens.)