confilict between two plugins - wordpress

I have two plugins (local code - not downloads) that import data from XML files.
The first plugin works fine. I then duplicated the plugin to work on a completely separate XML file. I renamed the directory/plugin-name/plugin-code.
When I activate the second plugin two weird things happen. The original plugin tries to activate the import code for the second plugin - how it does that I have no idea. Secondly, the second plugin gives a 500 error after it completes.
I deactivated the first plugin and tried the second plugin and it works fine. After re-activating the first plugin it fails.
Is there something in wordpress that causes this sort of conflict?
I know this question is not specifically code based but I guess the answer is.
I am just not smart enough to know how wordpress would create this conflict.

Unfortunately, You can't use same plugin copy. Because of how programming languages work, loading the same plugin twice will result in both versions trying to define the same functions and classes. PHP won't stand for that, and will die with a fatal error.
If you want to use the plugin before that you have rename classes and function of that plugin file. A main reason for check existence of function.
if ( ! function_exists( 'my_awesome_function' ) ) {
/**
* My Awesome function is awesome
*
* #param array $args
* #return array
*/
function my_awesome_function( $args ) {
//function stuff
return array();
}
}
In above code this will check if function not exist then declare it.

A plugin is basically a series of functions. By duplicating your plugin you are duplicating your functions which is causing the conflict. If you want to duplicate your plugin you won’t just need to duplicate and rename the folder but you’ll also need to rename your functions in the second plugin, otherwise you’ll be redeclaring already declared functions.

Related

Hook into the WordPress Theme Customizer save action

I am facing the following issue :
I used to keep all the styling in a theme options page. When the user clicked the save button, i had a backend script that generated a css file with the changes so that they will not be output inline in each page. This has a lot of benefits, amongst them caching.
I have switched to the Theme Customizer, and everything is fine except i can't find a way to hook into the the "save" button. I would like to trigger a function that updates the content of the css file when that button is clicked in the backend.
Is this even possible ?
Thanks !
Since WordPress 3.6.0 you can now call customize_save_after.
<?php
function emailAdmin(){
mail('your#email', 'Woza!', 'You won\'t believe this but someone has updated the theme customizations!!');
}
add_action( 'customize_save_after', 'emailAdmin' );
?>
More info: http://developer.wordpress.org/reference/hooks/customize_save_after/
I'm facing the same situation. The customize_save works BEFORE the options are saved, so that's out. I've emailed Otto (ottodestruct.com) about it.
The solution I have right now is as follows:
add_action('customize_save', 'regenCSS', 100);
function regenCSS( $wp_customize ) {
checkCSSRegen(); // Checks if I need to regen and does so
set_theme_mod('regen-css', time()+3); // Waits 3 seconds until everything is saved
}
function checkCSSRegen() {
if (get_theme_mod('regen-css') != "" && get_theme_mod('regen-css') < time()) {
makecss();
remove_theme_mod('regen-css');
}
}
I also add an extra checkCSSRegen(); to my customize_controls_init function.
Again, this is a little bit of a hack. Unfortunately, it's the best I can find to do at the time.
Another option would be to use a ajax response that just pings a php file. That feels even more of a hack than this.
Another quick hack would be to do a javascript action that when the save button is clicked, it sets a timer to delay a call to a PHP file that runs the compile. That is VERY hacky to me.
The only fallback of the above, is unless the customizer is reloaded or another value saved, you may not get all the values you want.
Anyone else have a better idea?
** Update **
Just added the following request to the Wordpress team. Hopefully we'll get it squeezed in there.
http://wordpress.org/ideas/topic/do-customize_save-action-hook-after-the-settings-are-saved?replies=3#post-24853
* Update 2 *
Looks like it will be in the 3.6 release as customize_save_after. Guess a few tweets and example code can make stuff happen even with the Wordpress team. ;)
As describe by #Dovy already you can hook customize_save_after to do this now:
do_action('customize_save_after', 'savesettings', 99);
When savesettings save settings to a file it will be bad practice to do this with native php file functions (like file_put_contents()) as described here: http://ottopress.com/2011/tutorial-using-the-wp_filesystem/ by #otto.
Solution for file saving will be to use wp_filesystem. To use wp_filesystem you will need the file credentials (ftp) of the user.
customize_save_after will be called in a AJAX request and the result won't be visible. Cause of the AJAX handle you can't ask the user for the file credentials which requires a form submit.
Solution can be found by saving the file credentials to wp-config.php and add them ( temporary ) to the database. Doing this savesettings can read the credentials from the database and use them to save the file by using credentials. (this solution is described in more detail here: https://wordpress.stackexchange.com/a/126631/31759)
Not tested, but there is the action hook customize_save in /wp-includes/class-wp-customize-manager.php.
It's inside the save() function:
/**
* Switch the theme and trigger the save action of each setting.
*
* #since 3.4.0
*/
There are some other interesting action hooks (do_action) in this file that may be worth check.

How do I target a SPECIFIC template with is_page_template() in Wordpress 3+

I'm running Wordpress 3.2 and I need a conditional to test for a certain template. My template is a file called special_offer.php. The name of the template is "Special Offer". I've tried both of the following:
is_page_template('special_offer.php');
is_page_template('Special Offer');
Neither of these work! The documentation clearly says that the parameter should be a string with the name of the file so I'm not sure what's wrong. I know the function is at least partially working because if I don't include a parameter, it returns true for any pages using templates (as expected).
AHA!
I solved this by adding wp_reset_query() just before the conditional.
I had already read warnings about many WP conditionals not working inside the loop. However, my conditional is NOT inside a loop. Turns out, it won't work even AFTER the loop, which is where mine was. So you you need to reset the query before you call it.
It's worth double checking which file is being used according to the WordPress template hierarchy [image].
Try adding this magic constant to the template file you think is being called:
<?php echo(__FILE__); ?>
If you don't see a path and filename a different file is being used (refer to the template hierarchy diagram in the link above) or check which templates are available:
<?php print_r( get_page_templates() ); ?>
(^ looks like this has to be called from the admin interface e.g. in a plugin)
Also, the documentation seems to state that is_page_template() won't work in the Loop without calling another function first.

Drupal7: Trying to theme a specific page using a preprocess function, but...I get a blank screen instead

I've just discovered that if you want to alter a specific page (or group of pages) all you need is to add templates file to the core templates. For instance, I need to theme my /helloword page using a page--helloworld.tpl.php and node--helloworld.tpl.php template files.
Now all I get is a blank screen so I tried to write a preprocess function that adds support for custom theme files like:
<?php
/**
* Adding or modifying variables before page render.
*/
function phptemplate_preprocess_page(&$vars) {
// Page change based on node->type
// Add a new page-TYPE template to the list of templates used
if (isset($vars['node'])) {
// Add template naming suggestion. It should alway use doublehyphens in Drupal7.
$vars['template_files'][] = 'page--'. str_replace('_', '-', $vars['node']->type);
}
}
?>
I see no syntax error but I still get a blank screen. Still no luck
Is someone able to figure out what's wrong in the code/routine?
Drupal7 + Omega Sub-Theme
Kind Regards
I think there's a tiny bit of confusion here: a template file named node--type.tpl.php will automatically be called for any node which has the type type...you don't need to add the template suggestions in yourself.
There is one caveat to this, you have to copy the original node.tpl.php to your theme folder and clear your caches otherwise Drupal won't pick it up.
Also you don't want to use the phptemplate_ prefix...rather you want your function to be called MYTHEMENAME_preprocess_page.
Your code to add the page template based on the node type looks spot on, see if you still have the problem after you change your function name and clear the caches.
Hope that helps :)

Wordpress plugin functions: check if function exists

I'm developing a Wordpress site that relies on a plugin to be activated for the site to function properly.
The plugin has a few useful functions that I'm using in the site's template files. When the plugin is active, everything works perfectly. If the plugin is deactivated, the content doesn't load.
Wrapping these functions in if(function_exists(...) obviously fixes that, but I'm wondering if there's a cleaner way of doing that in Wordpress. Is there a function that can be placed in the theme's functions.php file that can check if these functions are available every time I call them, and if not provide a safe fallback without me having to wrap them in the function_exists()?
Thanks.
If you're only using it sparingly (1-2 times), use if( function_exists() ). If you're calling the function several times through in different template files, I'd suggest using something like
In your functions.php
function mytheme_related_posts( $someparams = nil ) {
if( function_exists( 'related_posts' ) ) {
related_posts( $someparams );
} else {
echo 'Please enable related posts plugin';
}
}
Then use mytheme_related_posts() in your template.
I think this is the most clear way. It prevents all problems. I think you can write a function instead which can check if these functions are available every time you call them, but I'm almost sure it can cause you more trouble and it burns more memory then simply using if(function_exist()). Don't forget the else branch and it will work fine.
If you want to check if a plugin is active then you should be using the is_plugin_active() function - you can find the docs at: http://codex.wordpress.org/Function_Reference/is_plugin_active
You can then also use if(function_exists()) as well just to doubly make sure :)

Why is Drupal not aware of my template file?

this is a question how to override themable items in Drupal 6.
According to the book "Pro Drupal Development", we can override themable items in two ways:
overriding via Theme functions
overriding via Template files
So for example, in order to reformat the breadcrumb, I can:
via function theme_breadcrumb($breadcrumb)
via breadcrumb.tpl.php
But on my local testing server, the second approach (i.e. via template file) is not working! I see no breadcrumbs at all, while the first approach works fine.
Any idea how could this happen? any special settings I need to configure my Drupal?
thanks!
My custom theme "greyscale":
sites\all\themes\custom\greyscale:
- breadcrumb.tpl.php
- greyscale.info
- node.tpl.php
- page.tpl.php
- style.css
- template.php
relevant file contents:
* template.php:
function phptemplate_preprocess_breadcrumb(&$variables) {
$variables['breadcrumb_delimiter'] = '#';
}
breadcrumb.tpl.php:
Theme functions are setup to either use a template or a function to generate the markup, it will never use both as that's pointless.
For a theme function to use a template, it must be defined when you define it in hook_theme.
A template + preprocess function and a theme function really does the same thing: produce markup. It depends on the situation which method is best to use, that's why we have two. The good thing about templates, is that it allows themers to change the markup, without know much about PHP or Drupal.
Cache
Drupal caches all templates and theme functions defined in your theme, when you create new ones, you need to clear the cache, this can be done by:
Use drush
Clearing cache in admin/settings/performance
Use devel to clear it on each page load. Usable during development, biut will kill performance.
Switching theme back and forth will work too, but it really not the desired way to do it.
I personally always find it easier to alter breadcrumbs through template.php using hook_breadcrumb()
function mytheme_breadcrumb($breadcrumb) {
$sep = ' > ';
if (count($breadcrumb) > 0) {
return implode($breadcrumb, $sep) . $sep;
}
else {
return t("Home");
}
}
Any particular reason why you wish to use a .tpl.php file?

Resources