Title: Load PHP Function after page load
Last modified: December 28, 2018

---

# Load PHP Function after page load

 *  [dansperfect](https://wordpress.org/support/users/dansperfect/)
 * (@dansperfect)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/load-php-function-after-page-load/)
 * Ok so the function below is specifically for the official Woocommerce points 
   and Rewards plugin from there website. They have provided this as an example 
   to add to a plugin I may be building. I’m not there yet with my wordpress PHP
   skill and need some help. I want to make this function load after a webpage has
   succesfully loaded. I’m going to be using a Mailchimp form and have the form 
   on success reroute to a page with information and would like to reward to user
   points for joining the newsletter. Can someone please show me how to correctly
   either place this in the page or call it with javascript.
 *     ```
       <?php
       // Add the action setting
       add_filter( 'wc_points_rewards_action_settings', 'points_rewards_newsletter_action_settings' );
       function points_rewards_newsletter_action_settings( $settings ) {
   
       	$settings[] = array(
       		'title'    => __( 'Points earned for newsletter signup' ),
       		'desc_tip' => __( 'Enter the amount of points earned when a customer signs up for a newsletter via MailChimp.' ),
       		'id'       => 'wc_points_rewards_mailchimp_newsletter_signup',
       	);
       	return $settings;
       }
       // add the event descriptions
       add_filter( 'wc_points_rewards_event_description', 'add_points_rewards_newsletter_action_event_description', 10, 3 );
       function add_points_rewards_newsletter_action_event_description( $event_description, $event_type, $event ) {
       	$points_label = get_option( 'wc_points_rewards_points_label' );
       	// set the description if we know the type
       	switch ( $event_type ) {
       		case 'mailchimp-newsletter-signup': $event_description = sprintf( __( '%s earned for newsletter signup' ), $points_label ); break;
       	}
       	return $event_description;
       }
       // perform the event (of course this depends on your particular plugin/action)
       add_action( 'wp_mailchimp_new_newsletter_signup', 'points_rewards_newsletter_signup_action' );
       function points_rewards_newsletter_signup_action( $newsletter_id ) {
       	// can't give points to a user who isn't logged in
       	if ( ! is_user_logged_in() )
       		return;
       	// get the points configured for this custom action
       	$points = get_option( 'wc_points_rewards_mailchimp_newsletter_signup' );
       	if ( ! empty( $points ) ) {
       		// arbitrary data can be passed in with the points change, this will be persisted to the points event log
       		$data = array( 'newsletter_id' => $newsletter_id );
   
       		WC_Points_Rewards_Manager::increase_points( get_current_user_id(), $points, 'mailchimp-newsletter-signup', $data );
       	}
       }
       ```
   
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fload-php-function-after-page-load%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/load-php-function-after-page-load/#post-11031498)
 * It is two filters and 1 action, all related to Woocommerce, so you don’t need
   to load it in the page. You just need to load it (activate your plugin) and it
   will get called when Woo does those things.
 * [https://codex.wordpress.org/Plugin_API](https://codex.wordpress.org/Plugin_API)
 *  Thread Starter [dansperfect](https://wordpress.org/support/users/dansperfect/)
 * (@dansperfect)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/load-php-function-after-page-load/#post-11031539)
 * Well the two filters give an admin option to turn the function on or off while
   giving user a description of the each setting you create for it. The add_action
   is what I truly need yes but i have read that page several times and dont understand
   how to make that function occur on a page loading. I need this specifically to
   occur when a specific page loads and Woo doing its thing this will never occur
   as the plugin out the box only comes with registration, purchase, and reviews
   point earnings nothing to allow me to have occur on a user landing on a page 
   or doing a specific action other then those mentioned.
 * Sorry this is why im asking for help as the codex makes no sense to me for what
   im trying to do.
 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/load-php-function-after-page-load/#post-11032943)
 * This part of the code
 *     ```
       // perform the event (of course this depends on your particular plugin/action)
       add_action( 'wp_mailchimp_new_newsletter_signup', 'points_rewards_newsletter_signup_action' );
       ```
   
 * indicates that you are adding to an existing plugin. If it is calling that action
   during signup, then your function will be called. If there is not an action being
   called already, you can’t `add_action`. (Well, you could but it won’t be executed.)
   
   Search the code of the plugin for do_action, and see what actions are called.
   Use the one for the point at which your code should execute.
 *  Thread Starter [dansperfect](https://wordpress.org/support/users/dansperfect/)
 * (@dansperfect)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/load-php-function-after-page-load/#post-11033734)
 * I can do that thank you so much

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Load PHP Function after page load’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 4 replies
 * 2 participants
 * Last reply from: [dansperfect](https://wordpress.org/support/users/dansperfect/)
 * Last activity: [7 years, 5 months ago](https://wordpress.org/support/topic/load-php-function-after-page-load/#post-11033734)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
