Theme Action to hook for one time only function - wordpress

I am developing a custom theme. After my theme is activated/installed I need to run a one time only function that will set some options. What is the best action to hook for this?
Should I use after_setup_theme? My understanding is that this event/action fires on each page load so I don't think I should use this action correct?
*Ps: is it best to place this code in my functions.php file or somewhere else? (I am using the underscores theme template).

You should look at after_switch_theme action. It'll fired only on your theme activation( just one time ). So, your one time function will look:
add_action('after_switch_theme', 'my_one_time_function');
function my_one_time_function () {
//Your code
}
Code goes to functions.php file.
P.S. If you want to run some function before deactivating theme, you can use switch_theme hook. It has same use as after_switch_theme.

Related

Running PHP code before loading template in Wordpress

Since I've only started using Wordpress recently, I'm still trying to figure how to run PHP code prior to loading the template for a particular post.
I'm talking about code which would be executed in the contoller class in a MVC structure.
Obviously it's tempting to stuff it in the page's template file, but I'm sure this won't be exactly qualified as best practice.
Any suggestions on this matter? Many thanks.
The actions(list of executing) on front part WordPress:
muplugins_loaded
registered_taxonomy
registered_post_type
plugins_loaded
sanitize_comment_cookies
setup_theme
load_textdomain
after_setup_theme
auth_cookie_malformed
auth_cookie_valid
set_current_user
init
widgets_init
register_sidebar
wp_register_sidebar_widget
wp_default_scripts
wp_default_styles
admin_bar_init
add_admin_bar_menus
wp_loaded
parse_request
send_headers
parse_query
pre_get_posts
posts_clauses
posts_selection
wp
template_redirect
get_header
wp_head
wp_enqueue_scripts
wp_print_styles
wp_print_scripts
get_search_form
loop_start
the_post
get_template_part_content
loop_end
get_sidebar
dynamic_sidebar
get_search_form
pre_get_comments
wp_meta
get_footer
get_sidebar
wp_footer
wp_print_footer_scripts
admin_bar_menu
wp_before_admin_bar_render
wp_after_admin_
bar_render
shutdown
So, if You need some magic without all core functions, You can put your code into own mu-plugin for example, and it will execute on start(muplugins_loaded action).
For basic functionality and with theme functions - wp_loaded, etc. see the list above.
Usual, uses init action(cause WordPress is fully loaded, but without header and other stuff), example:
add_action( 'init', 'my_func' );
function my_func() {
// Write some code here...
}

WordPress using different CSS - is this possible?

Bit is a basic question here but can someone confirm that this statement be confirmed: WordPress Pages (certain templates created within) can pull different CSS and JS?
Or - does WordPress only permit universal CSS + JS to be pulled across the entire site?
Thanks for clearing this up.
Depends on what plugin and themes you use. The WordPress/PHP functions wp_enqueue_style() and wp_enqueue_script() can be used literally by everyone (core, themes, plugins, you) to request WordPress to load styles or JavaSctript. You can combine this with WordPress functions to check whether the current page is something you want to filter for (post type, post, front-page, category archive, template, etc.). Here is an example to load a custom style if on front page :
if (is_front_page()) {
wp_enqueue_style('custom-frontpage', 'my/path/to/frontpage.css');
}
You will have to hook this piece of code to the wp_enqueue_script action so that WordPress executes it at the appropriate time. Here is an example using an anonymous function:
add_action('wp_enqueue_scripts', function() {
if (is_front_page())
wp_enqueue_style('custom-frontpage', 'my/path/to/frontpage.css');
});
You can also register your code as a "normal" function and pass the functions name to add_action() instead.
Edit: Enabling and disabling plugins is a bit more difficult, since you can never know how they implement their features without examining the source code. Here are my thoughts on this:
The plugin likely uses the above method (wp_enqueue_styles, wp_enqueue_scripts) to register it's styles and scripts. The plugin, since it assumes to be needed on all pages and posts, does this on every page without the conditional checking described earlier.
You could do one of the following to stop the plugin from doing this:
Identify the place where the plugin loads the styles and scripts and add the if-statement to only do so if the post-ID matches your desired post-ID. This method is bad since your changes are lost every time the plugin is updated.
Write a "counter plugin" (you could just add it to your theme or find a plugin that allowes you to add PHP to your page) that "dequeues" the style and script added by the plugin with inversed conditional tag
The counter-plugin approach would look as follows:
function custom_unregister_plugin() {
if (not the desired blog post) {
wp_dequeue_style('my-plugin-stylesheet-handle');
wp_dequeue_script('my-plugin-script-handle');
}
}
Make sure this function is executed after the enqueuing-code of your plugin by giving it a low priority in the same hook (999 is just an example, test it yourself):
add_action('wp_enqueue_scripts', 'custom_unregister_plugin', 999);
With wp_enqueue_style() you can add stylesheet (https://developer.wordpress.org/reference/functions/wp_enqueue_style/)
You can use it after detecting which template is used
function enqueue_custom_stylesheet() {
if(get_page_template() == 'contact.php')
wp_enqueue_style( 'contact-style', get_template_directory_uri().'/contact.css' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_custom_stylesheet' );
You can use wp_enqueue_style for your CSS, wp_enqueue_script for your JS, wp_localize_script to pass variables from PHP to JS.
You can call these with hooks like:
funtion enqueue_my_stuff()
{
// your enqueue function calls
}
add_action('wp_enqueue_scripts','enqueue_my_stuff'); //front end
add_action('admin_enqueue_scripts','enqueue_my_stuff'); //admin panel
add_action('login_enqueue_scripts','enqueue_my_stuff'); //login screen

(WP) Override Plugin Function In Theme Functions

How to override function inside plugin and put in theme functions.php file? I've try using remove_filter and add_filter, apply_filter, add_action etc, but still not worked.
https://github.com/mikejolley/WP-Job-Manager/blob/master/wp-job-manager-template.php#L72
I want to override function on line 72. How can I done this? I don't want to edit plugin files because I just want this function replaced only when this theme activated. If not activated, function will be as the original function.
This function used by plugin. And no any filters or actions inside. So, my sorry, you can't solve this issue.

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"

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

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.

Resources