Fire an action right after Appearance > Theme Options has been saved - wordpress

I just started working with Wordpress (v. 3.6.1).
I have OptionTree installed and as it seems it handles the Theme Options page. I want to run my function (in a plugin or wherever else) right after the user saves the changes of this page.
So far I found out that option-tree/includes/ot-settings-api.php generates the form and it sets the form action to options.php (which is a wordpress core file). I was thinking about change the action to my custom php file and handle the save procedure and finally runs my own function. But this solution looks pretty ugly.
I wonder if there's another way to get the job done.
Thanks.

Thanks to #Sheikh Heera link (tutsplus) I could find a solution.
I think this is some kind of hack and I still don't know if it is the best way. Anyway I did this:
Create a file your-theme-settings.php in your theme lib folder.
Let Wordpress knows about your file by adding this code in your theme functions.php:
include_once('lib/your-theme-settings.php');
Add this code to your-theme-settings.php:
function your_theme_register_settings() {
register_setting('option_tree', 'option_tree', 'your_theme_validate_options');
}
function your_theme_validate_options($input) {
// do whatever you have to do with $input.
}
add_action('admin_init', 'your_theme_register_settings');
In step 3, I put 'option_tree' as 1st and 2nd argument of register_settings function, because I noticed that the Option Group and Option Name of OptionTree plugin is option_tree.
I'm not sure if this is the best solution, so I would be glad if you shares your ideas.

Related

How to implement a WordPress Action Hook

I'm using the All-In-One Video Player plugin and want to alter its behaviour by listening to events that the player emits and taking actions based on them.
I contacted plugin's support team and got a very good response that I'm sure would mean something to someone who understands WordPress - I'm not one of those people.
The support team suggested using the action hook aiovg_player_footer. It looks like I have to implement that function, but I have no idea where to write that code. Is there a specific file that I need to create / update in order to get implement this function.
My function will need to alter the HTML that the plugin produces. Is this just a case of doing something like
echo '<script>console.log("helo");</script>' ?
You should add the following code in functions.php file located in the root of your theme directory:
function so61638829_aiovg_player_footer()
{
// Do something
}
add_action('aiovg_player_footer', 'so61638829_aiovg_player_footer');

Drupal Webform hook_webform_submission_insert not firing

I am trying to use the hook_webform_submission_insert in my theme template.php file. I have 2 other webform hooks currently running in here and they work just fine. I am trying to get the submission data after it has been submitted. Below is my code.
function acquarius_hook_webform_submission_insert($node, $submission){
var_dump($node);
var_dump($submission);
}
I am sure I am missing something small here but everything I try seems to fail.
You need to replace hook keyword with your theme name. Also add hook functions in module.
YOURTHEMENAME_webform_submission_insert($node, $submission){
// give your code here
}
After you create a new hook, you need to clear the drupal caches.
Go to YOURSITE.com/admin/config/development/performance
And click on "Clear all caches"

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.

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 :)

Insert a plugin manually into wordpress page

I am working in worpress front page.
I want to add a plugin to the page at a specific location manually but adding the code to the page myself.
I basically want to include a plugin in a certain page on a certain location. So I'm create a div...
<div id="plugin-holder">
**Plugin-will-appear-here-with-this-code**
</div>
Don't anyone know how this is done please?
Thanks
If you're wanting a plugin to appear somewhere, you'll be looking for "shortcode" functionality.
This is actually surprisingly easy to code, check out the examples in the Codex under Shortcode API - ie:
function bartag_func( $atts ) {
// ... do more things here ...
return "text to replace shortcode";
}
add_shortcode( 'bartag', 'bartag_func' );
Once you've called these functions you can use [bartag] in code and it will run your function and replace the shortcode with the generated text your function returns.
If you're adding shortcode functionality to your site, it generally makes most sense to code a really simple plugin and put it in that. The reason why this works best is that, over time, it's really easy to forget and upgrade a theme by mistake (or even change to a new theme) and thus break your site by losing your custom code in your former functions.php. Surprisingly, this is pretty easy to achieve and only requires some specially formatted comments at the top of your plugin file and a little common sense in coding - there are many tutorials and "how to"s around!
Here's a useful shortcode tutorial: http://www.reallyeffective.co.uk/archives/2009/06/22/how-to-code-your-own-wordpress-shortcode-plugin-tutorial-part-1/
You should add the relevant plugin code to functions.php.
I suspect you'll want to use some conditional tags, like is_home() to pinpoint your location. But maybe not, depending on what you are trying to do,
Also, if you're trying to to insert from a pre-existing plug-in, make sure you remove the register_activation_hook or activate_pluginname action.
If your plugin supports a sidebar widget you can simply "widgitize" the div tag that you wish to insert the plugin into.. Google the term and you are gonna find many resources.

Resources