How to call a functions/classes without using shortcodes | Wordpress? - php

So i have a question - regarding WordPress plugins specifically shortcodes
Additional Info >
CMS : Wordpress (5.7.1)
Language : PHP 7< , JS
Plugin : Ultimate Member
i looking for a way to call the profile functions outside the plugin __DIR__ into the wordpress theme ( 2021 theme ) without using the plugin shortcodes (ultimate member )
-just to call it manually in custom template in theme folder
why ?
i built a custom JS,HTML5,CSS webpage and i want to call
[ username , email , user account information ]
profile shortcode ( ultimate member ) == " [ultimatemember form_id="76"] "
OUTPUT :
A Picture Of The Profile Portal
Everything works okay
Now how can i call that profile portal without the short code ?
here's what i tried to do
UMDIR = Ultimate Member Plugin Directory
UMDIR : wp-content\plugins\ultimate-member\includes\core\class-shortcodes.php
# Shortcode
558: function ultimatemember( $args = array() ) {
559:
560: return $this->load( $args );
561: }
return $this->load( $args );
This Line Is Responsible to load the functions that Ultimate Member Need to run the profile portal
#Load a module with global function
569: function load( $args ) {
570: $defaults = array();
571: $args = wp_parse_args( $args, $defaults );
572:
573: // when to not continue
$this->form_id = isset( $args['form_id'] ) ? $args['form_id'] : null;
if ( ! $this->form_id ) {
return;
}
$this->form_status = get_post_status( $this->form_id );
if ( $this->form_status != 'publish' ) {
return;
}
// get data into one global array
$post_data = UM()->query()->post_data( $this->form_id );
$args = array_merge( $args, $post_data );
ob_start();
/**
* UM hook
*
* #type filter
* #title um_pre_args_setup
* #description Change arguments on load shortcode
* #input_vars
* [{"var":"$post_data","type":"string","desc":"$_POST data"}]
* #change_log
* ["Since: 2.0"]
* #usage
* <?php add_filter( 'um_pre_args_setup', 'function_name', 10, 1 ); ?>
* #example
* <?php
* add_filter( 'um_pre_args_setup', 'my_pre_args_setup', 10, 1 );
* function my_pre_args_setup( $post_data ) {
* // your code here
* return $post_data;
* }
* ?>
*/
$args = apply_filters( 'um_pre_args_setup', $args );
if ( ! isset( $args['template'] ) ) {
$args['template'] = '';
}
if ( isset( $post_data['template'] ) && $post_data['template'] != $args['template'] ) {
$args['template'] = $post_data['template'];
}
if ( ! $this->template_exists( $args['template'] ) ) {
$args['template'] = $post_data['mode'];
}
if ( ! isset( $post_data['template'] ) ) {
$post_data['template'] = $post_data['mode'];
}
if ( 'directory' == $args['mode'] ) {
wp_enqueue_script( 'um_members' );
if ( is_rtl() ) {
wp_enqueue_style( 'um_members_rtl' );
} else {
wp_enqueue_style( 'um_members' );
}
}
if ( 'directory' != $args['mode'] ) {
$args = array_merge( $post_data, $args );
if ( empty( $args['use_custom_settings'] ) ) {
$args = array_merge( $args, $this->get_css_args( $args ) );
} else {
$args = array_merge( $this->get_css_args( $args ), $args );
}
}
// filter for arguments
/**
* UM hook
*
* #type filter
* #title um_shortcode_args_filter
* #description Change arguments on load shortcode
* #input_vars
* [{"var":"$args","type":"string","desc":"Shortcode arguments"}]
* #change_log
* ["Since: 2.0"]
* #usage
* <?php add_filter( 'um_shortcode_args_filter', 'function_name', 10, 1 ); ?>
* #example
* <?php
* add_filter( 'um_shortcode_args_filter', 'my_shortcode_args', 10, 1 );
* function my_shortcode_args( $args ) {
* // your code here
* return $args;
* }
* ?>
*/
$args = apply_filters( 'um_shortcode_args_filter', $args );
/**
* #var string $mode
*/
extract( $args, EXTR_SKIP );
//not display on admin preview
if ( empty( $_POST['act_id'] ) || $_POST['act_id'] != 'um_admin_preview_form' ) {
if ( 'register' == $mode && is_user_logged_in() ) {
ob_get_clean();
return __( 'You are already registered', 'ultimate-member' );
}
}
// for profiles only
if ( $mode == 'profile' && um_profile_id() ) {
//set requested user if it's not setup from permalinks (for not profile page in edit mode)
if ( ! um_get_requested_user() ) {
um_set_requested_user( um_profile_id() );
}
if ( ! empty( $args['use_custom_settings'] ) ) { // Option "Apply custom settings to this form"
if ( ! empty( $args['role'] ) ) { // Option "Make this profile form role-specific"
// show the first Profile Form with role selected, don't show profile forms below the page with other role-specific setting
if ( empty( $this->profile_role ) ) {
$current_user_roles = UM()->roles()->get_all_user_roles( um_profile_id() );
if ( empty( $current_user_roles ) ) {
ob_get_clean();
return '';
} elseif ( is_array( $args['role'] ) ) {
if ( ! count( array_intersect( $args['role'], $current_user_roles ) ) ) {
ob_get_clean();
return '';
}
} else {
if ( ! in_array( $args['role'], $current_user_roles ) ) {
ob_get_clean();
return '';
}
}
$this->profile_role = $args['role'];
} else {
ob_get_clean();
return '';
}
}
}
}
/**
* UM hook
*
* #type action
* #title um_pre_{$mode}_shortcode
* #description Action pre-load form shortcode
* #input_vars
* [{"var":"$args","type":"array","desc":"Form shortcode pre-loading"}]
* #change_log
* ["Since: 2.0"]
* #usage add_action( 'um_pre_{$mode}_shortcode', 'function_name', 10, 1 );
* #example
* <?php
* add_action( 'um_pre_{$mode}_shortcode', 'my_pre_shortcode', 10, 1 );
* function my_pre_shortcode( $args ) {
* // your code here
* }
* ?>
*/
do_action( "um_pre_{$mode}_shortcode", $args );
/**
* UM hook
*
* #type action
* #title um_before_form_is_loaded
* #description Action pre-load form shortcode
* #input_vars
* [{"var":"$args","type":"array","desc":"Form shortcode pre-loading"}]
* #change_log
* ["Since: 2.0"]
* #usage add_action( 'um_before_form_is_loaded', 'function_name', 10, 1 );
* #example
* <?php
* add_action( 'um_before_form_is_loaded', 'my_pre_shortcode', 10, 1 );
* function my_pre_shortcode( $args ) {
* // your code here
* }
* ?>
*/
do_action( "um_before_form_is_loaded", $args );
/**
* UM hook
*
* #type action
* #title um_before_{$mode}_form_is_loaded
* #description Action pre-load form shortcode
* #input_vars
* [{"var":"$args","type":"array","desc":"Form shortcode pre-loading"}]
* #change_log
* ["Since: 2.0"]
* #usage add_action( 'um_before_{$mode}_form_is_loaded', 'function_name', 10, 1 );
* #example
* <?php
* add_action( 'um_before_{$mode}_form_is_loaded', 'my_pre_shortcode', 10, 1 );
* function my_pre_shortcode( $args ) {
* // your code here
* }
* ?>
*/
do_action( "um_before_{$mode}_form_is_loaded", $args );
$this->template_load( $template, $args );
$this->dynamic_css( $args );
if ( um_get_requested_user() || $mode == 'logout' ) {
um_reset_user();
}
/**
* UM hook
*
* #type action
* #title um_after_everything_output
* #description Action after load shortcode content
* #change_log
* ["Since: 2.0"]
* #usage add_action( 'um_after_everything_output', 'function_name', 10 );
* #example
* <?php
* add_action( 'um_after_everything_output', 'my_after_everything_output', 10 );
* function my_after_everything_output() {
* // your code here
* }
* ?>
*/
do_action( 'um_after_everything_output' );
$output = ob_get_clean();
813: return $output;
814: }
And just to clearfiy all of them inside a namespace called
namespace um\core;
and inside a class Shortcodes
so when i try and call ( still testing in the same folder ) ( inside the class )
ultimatemember();
this error shows up
syntax error, unexpected identifier "ultimatemember", expecting "function" or "const" in
HOW ?
expecting "function"
Isn't ultimatemember(); a function ??
and if i called it outside the class it return as undefined function
Uncaught Error: Call to undefined function um\core\ultimatemember()

This question has been answered by #amarinediary
You have to call the class variable name before the method
like this
class Shortcode{
function runme{
//code
}}
this is how you call it
$classin = new Shortcodes();
$classin-> ultimatemember();

Related

Add action in theme quick view Wordpress Woocommerce

Got stucked. I need to insert a code in theme quick view - the action is : do_action( 'iworks_omnibus_wc_lowest_price_message' );
THE CODE IS:
<?php
/**
* Display product quickview.
*
* #package Razzi
* #version 1.0.0
*/
use Razzi\Helper;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $product;
$classes = wc_get_product_class( '', $product );
if ( Helper::get_option( 'product_add_to_cart_ajax' ) ) {
$classes[] = 'product-add-to-cart-ajax';
}
if ( get_option( 'rz_buy_now' ) == 'yes' ) {
$classes[] = 'has-buy-now';
}
$classes[] = 'product-is-quickview';
do_action('woocommerce_before_single_product');
?>
<div class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>">
<div class="entry-thumbnail">
<?php
/**
* Hook: razzi_woocommerce_product_quickview_thumbnail
*
* #hooked woocommerce_show_product_sale_flash - 5
* #hooked woocommerce_show_product_images - 10
* #hooked product_quick_view_more_info_button - 15
*
*/
do_action( 'razzi_woocommerce_product_quickview_thumbnail' );
?>
</div>
<div class="summary entry-summary razzi-scrollbar">
<?php
/**
* Hook: razzi_woocommerce_product_quickview_summary
*
* #hooked woocommerce_template_single_rating - 10
* #hooked woocommerce_template_single_title - 20
* #hooked open_price_box_wrapper - 30
* #hooked woocommerce_template_single_price - 40
* #hooked product_availability - 50
* #hooked close_price_box_wrapper - 60
* #hooked woocommerce_template_single_excerpt - 70
* #hooked woocommerce_template_single_add_to_cart - 80
* #hooked woocommerce_template_single_meta - 90
*
*/
do_action( 'razzi_woocommerce_product_quickview_summary' );
?>
</div>
</div>
<?php
and
<?php
/**
* WooCommerce Quick View template hooks.
*
* #package Razzi
*/
namespace Razzi\WooCommerce\Modules;
use Razzi\Helper;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Class of Product Quick View
*/
class Quick_View {
/**
* Instance
*
* #var $instance
*/
protected static $instance = null;
/**
* Initiator
*
* #since 1.0.0
* #return object
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Instantiate the object.
*
* #since 1.0.0
*
* #return void
*/
public function __construct() {
// Quick view modal.
add_action( 'wc_ajax_product_quick_view', array( $this, 'quick_view' ) );
add_action( 'razzi_woocommerce_product_quickview_thumbnail', 'woocommerce_show_product_images', 10 );
add_action( 'razzi_woocommerce_product_quickview_thumbnail', array(
$this,
'product_quick_view_more_info_button'
) );
add_action( 'razzi_woocommerce_product_quickview_summary', 'woocommerce_template_single_rating', 10 );
add_action( 'razzi_woocommerce_product_quickview_summary', 'woocommerce_template_single_title', 20 );
add_action( 'razzi_woocommerce_product_quickview_summary', array(
$this,
'open_price_box_wrapper'
), 30 );
if ( apply_filters( 'razzi_product_show_price', true ) ) {
add_action( 'razzi_woocommerce_product_quickview_summary', 'woocommerce_template_single_price', 40 );
}
add_action( 'razzi_woocommerce_product_quickview_summary', array(
\Razzi\WooCommerce\Helper::instance(),
'product_availability'
), 50 );
add_action( 'razzi_woocommerce_product_quickview_summary', array(
$this,
'close_price_box_wrapper'
), 60 );
add_action( 'razzi_woocommerce_product_quickview_summary', 'woocommerce_template_single_excerpt', 70 );
add_action( 'razzi_woocommerce_product_quickview_summary', 'woocommerce_template_single_add_to_cart', 80 );
add_action( 'razzi_woocommerce_product_quickview_summary', 'woocommerce_template_single_meta', 90 );
add_action( 'wp_footer', array( $this, 'quick_view_modal' ), 40 );
}
/**
* Open button wrapper
*
* #since 1.0.0
*
* #return void
*/
public function open_price_box_wrapper() {
echo '<div class="summary-price-box">';
}
/**
* Close button wrapper
*
* #since 1.0.0
*
* #return void
*/
public function close_price_box_wrapper() {
echo '</div>';
}
/**
* Product quick view template.
*
* #since 1.0.0
*
* #return void
*/
public function quick_view() {
if ( empty( $_POST['product_id'] ) ) {
wp_send_json_error( esc_html__( 'No product.', 'razzi' ) );
exit;
}
$post_object = get_post( $_POST['product_id'] );
if ( ! $post_object || ! in_array( $post_object->post_type, array(
'product',
'product_variation',
true
) ) ) {
wp_send_json_error( esc_html__( 'Invalid product.', 'razzi' ) );
exit;
}
$GLOBALS['post'] = $post_object;
wc_setup_product_data( $post_object );
ob_start();
wc_get_template( 'content-product-quickview.php', array(
'post_object' => $post_object,
) );
wp_reset_postdata();
wc_setup_product_data( $GLOBALS['post'] );
$output = ob_get_clean();
wp_send_json_success( $output );
exit;
}
/**
* Quick view modal.
*
* #since 1.0.0
*
* #return void
*/
public function quick_view_modal() {
if( Helper::is_cartflows_template() ) {
return;
}
$featured_icons = (array) Helper::get_option( 'product_loop_featured_icons' );
if ( ! in_array( 'qview', $featured_icons ) ) {
return;
}
?>
<div id="quick-view-modal" class="quick-view-modal rz-modal single-product">
<div class="off-modal-layer"></div>
<div class="modal-content container woocommerce">
<div class="button-close active">
<?php echo \Razzi\Icon::get_svg( 'close' ) ?>
</div>
<div class="product"></div>
</div>
<div class="razzi-posts__loading">
<div class="razzi-loading"></div>
</div>
</div>
<?php
}
/**
* Quick view more info button
*
* #since 1.0.0
*
* #return void
*/
public function product_quick_view_more_info_button() {
printf(
'<a href="%s" class="product-more-infor">
<span class="product-more-infor__text">%s</span>%s
</a>',
is_customize_preview() ? '#' : esc_url( get_permalink() ),
apply_filters( 'product_quick_view_more_infor_text', esc_html__( 'More Product Info', 'razzi' ) ),
\Razzi\Icon::get_svg( 'infor', '', 'shop' )
);
}
}
Got many errors making customizations on that. Thanks in advance.
I was trying to show omnibus price from the plugin called Omnibus — show the lowest price by Marcin Pietrzak in the theme quick view.

WordPress: Follow user button not working when added inside Ajax pagination template?

I'm using an plugin that working with Ajax for creating a follow button to make our users following each other.
I putting the follow button anywhere that i need an user to follow the other users like that echo pwuf_get_follow_unfollow_links( //user id here );
The problem i'm facing is if i added this echo function inside any Ajax pagination template whatever post Ajax pagination or users list Ajax pagination, the follow button not working and can't find the user id on click!
But for sure he can find the user id on the first page before i loading more to the second page!
e.g: if i have an Ajax pagination like this one, and tried to echo the follow button function inside this ajax pagination not working!?
add_action('wp_ajax_loadmore_by_ajax', 'loadmore_by_ajax_callback');
add_action('wp_ajax_nopriv_loadmore_by_ajax', 'loadmore_by_ajax_callback');
function loadmore_by_ajax_callback() {
//do something
//echoing follow button
echo pwuf_get_follow_unfollow_links( //user id here );
}
I'll add all my plugin code below, and i hope to get some help to fix this problem, Thanks for your time.
follow.js
jQuery(document).ready(function($) {
/*******************************
follow / unfollow a user
*******************************/
$( '.follow-links a' ).on('click', function(e) {
e.preventDefault();
var $this = $(this);
if( pwuf_vars.logged_in != 'undefined' && pwuf_vars.logged_in != 'true' ) {
alert( pwuf_vars.login_required );
return;
}
var data = {
action: $this.hasClass('follow') ? 'follow' : 'unfollow',
user_id: $this.data('user-id'),
follow_id: $this.data('follow-id'),
nonce: pwuf_vars.nonce
};
$this.closest('.follow-links').find('img.pwuf-ajax').show();
$.post( pwuf_vars.ajaxurl, data, function ( response ) {
if ( response == 'success' ) {
if ( $this.hasClass( 'follow' ) ) {;
$this.removeClass( 'follow' ).addClass( 'unfollow' );
$this.find( 'span' ).text( 'Unfollow' );
} else {;
$this.removeClass( 'unfollow' ).addClass( 'follow' );
$this.find( 'span' ).text( 'Follow' );
}
} else {
alert( pwuf_vars.processing_error );
}
$this.closest('.follow-links').find('img.pwuf-ajax').hide();
});
});
});
actions.php
<?php
/**
* Ajax Actions
*
* #package User Following System
* #subpackage Ajax Actions
* #copyright Copyright (c) 2012, Pippin Williamson
* #license http://opensource.org/licenses/gpl-2.0.php GNU Public License
* #since 1.0
*/
/**
* Processes the ajax request to follow a user
*
* #access private
* #since 1.0
* #return void
*/
function pwuf_process_new_follow() {
if ( isset( $_POST['user_id'] ) && isset( $_POST['follow_id'] ) ) {
if( pwuf_follow_user( absint( $_POST['user_id'] ), absint( $_POST['follow_id'] ) ) ) {
echo 'success';
} else {
echo 'failed';
}
}
die();
}
add_action('wp_ajax_follow', 'pwuf_process_new_follow');
/**
* Processes the ajax request to unfollow a user
*
* #access private
* #since 1.0
* #return void
*/
function pwuf_process_unfollow() {
if ( isset( $_POST['user_id'] ) && isset( $_POST['follow_id'] ) ) {
if( pwuf_unfollow_user( absint( $_POST['user_id'] ), absint( $_POST['follow_id'] ) ) ) {
echo 'success';
} else {
echo 'failed';
}
}
die();
}
add_action('wp_ajax_unfollow', 'pwuf_process_unfollow');
display-functions.php
<?php
/**
* Retrieves the follow / unfollow links
*
* #access public
* #since 1.0
* #param int $user_id - the ID of the user to display follow / unfollow links for
* #return string
*/
function pwuf_get_follow_unfollow_links( $follow_id = null ) {
global $user_ID;
if( empty( $follow_id ) )
return;
if ( $follow_id == $user_ID )
return;
ob_start(); ?>
<div class="follow-links">
<?php
if ( pwuf_is_following( $user_ID, $follow_id ) ) {
$classes = "unfollow";
$text = "Following";
} else {
$classes = "follow";
$text = "Follow";
}
?>
<span><span><?php echo $text; ?></span></span>
<img src="<?php echo PWUF_FOLLOW_URL; ?>/images/loading.svg" class="pwuf-ajax" style="display:none;"/>
</div>
<?php
return ob_get_clean();
}
follow-functions.php
<?php
/**
* Follow Functions
*
* #package User Following System
* #subpackage Follow Functions
* #copyright Copyright (c) 2012, Pippin Williamson
* #license http://opensource.org/licenses/gpl-2.0.php GNU Public License
* #since 1.0
*/
/**
* Retrieves all users that the specified user follows
*
* Gets all users that $user_id followers
*
* #access private
* #since 1.0
* #param int $user_id - the ID of the user to retrieve following for
* #return array
*/
function pwuf_get_following( $user_id = 0 ) {
if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
}
$following = get_user_meta( $user_id, '_pwuf_following', true );
if ( empty( $following ) ) {
return;
}
return (array) apply_filters( 'pwuf_get_following', $following, $user_id );
}
/**
* Retrieves users that follow a specified user
*
* Gets all users following $user_id
*
* #access private
* #since 1.0
* #param int $user_id - the ID of the user to retrieve followers for
* #return array
*/
function pwuf_get_followers( $user_id = 0 ) {
if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
}
$followers = get_user_meta( $user_id, '_pwuf_followers', true );
if ( empty( $followers ) ) {
return;
}
return (array) apply_filters( 'pwuf_get_followers', $followers, $user_id );
}
/**
* Follow a user
*
* Makes a user follow another user
*
* #access private
* #since 1.0
* #param int $user_id - the ID of the user that is doing the following
* #param int $user_to_follow - the ID of the user that is being followed
* #return bool
*/
function pwuf_follow_user( $user_id, $user_to_follow ) {
$following = pwuf_get_following( $user_id );
if ( $following && is_array( $following ) ) {
$following[] = $user_to_follow;
} else {
$following = array();
$following[] = $user_to_follow;
}
// retrieve the IDs of all users who are following $user_to_follow
$followers = pwuf_get_followers( $user_to_follow );
if ( $followers && is_array( $followers ) ) {
$followers[] = $user_id;
} else {
$followers = array();
$followers[] = $user_id;
}
do_action( 'pwuf_pre_follow_user', $user_id, $user_to_follow );
// update the IDs that this user is following
$followed = update_user_meta( $user_id, '_pwuf_following', $following );
// update the IDs that follow $user_id
$followers = update_user_meta( $user_to_follow, '_pwuf_followers', $followers );
// increase the followers count
$followed_count = pwuf_increase_followed_by_count( $user_to_follow ) ;
if ( $followed ) {
do_action( 'pwuf_post_follow_user', $user_id, $user_to_follow );
return true;
}
return false;
}
/**
* Unfollow a user
*
* Makes a user unfollow another user
*
* #access private
* #since 1.0
* #param int $user_id - the ID of the user that is doing the unfollowing
* #param int $unfollow_user - the ID of the user that is being unfollowed
* #return bool
*/
function pwuf_unfollow_user( $user_id, $unfollow_user ) {
do_action( 'pwuf_pre_unfollow_user', $user_id, $unfollow_user );
// get all IDs that $user_id follows
$following = pwuf_get_following( $user_id );
if ( is_array( $following ) && in_array( $unfollow_user, $following ) ) {
$modified = false;
foreach ( $following as $key => $follow ) {
if ( $follow == $unfollow_user ) {
unset( $following[$key] );
$modified = true;
}
}
if ( $modified ) {
if ( update_user_meta( $user_id, '_pwuf_following', $following ) ) {
pwuf_decrease_followed_by_count( $unfollow_user );
}
}
}
// get all IDs that follow the user we have just unfollowed so that we can remove $user_id
$followers = pwuf_get_followers( $unfollow_user );
if ( is_array( $followers ) && in_array( $user_id, $followers ) ) {
$modified = false;
foreach ( $followers as $key => $follower ) {
if ( $follower == $user_id ) {
unset( $followers[$key] );
$modified = true;
}
}
if ( $modified ) {
update_user_meta( $unfollow_user, '_pwuf_followers', $followers );
}
}
if ( $modified ) {
do_action( 'pwuf_post_unfollow_user', $user_id, $unfollow_user );
return true;
}
return false;
}
/**
* Retrieve following count
*
* Gets the total number of users that the specified user is following
*
* #access private
* #since 1.0
* #param int $user_id - the ID of the user to retrieve a count for
* #return int
* #param int|string|WP_User $user_id User ID or object.
* #return bool Whether the user exists
*/
function pwuf_get_following_count( $user_id = 0 ) {
if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
}
$following = pwuf_get_following( $user_id );
$count = 0;
if ( $following ) {
$count = count( $following );
}
return (int) apply_filters( 'pwuf_get_following_count', $count, $user_id );
}
/**
* Retrieve follower count
*
* Gets the total number of users that are following the specified user
*
* #access private
* #since 1.0
* #param int $user_id - the ID of the user to retrieve a count for
* #return int
*/
function pwuf_get_follower_count( $user_id = 0 ) {
if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
}
$followed_count = get_user_meta( $user_id, '_pwuf_followed_by_count', true );
$count = 0;
if ( $followed_count ) {
$count = $followed_count;
}
return (int) apply_filters( 'pwuf_get_follower_count', $count, $user_id );
}
/**
* Increase follower count
*
* Increments the total count for how many users a specified user is followed by
*
* #access private
* #since 1.0
* #param int $user_id - the ID of the user to increease the count for
* #return int
*/
function pwuf_increase_followed_by_count( $user_id = 0 ) {
do_action( 'pwuf_pre_increase_followed_count', $user_id );
$followed_count = pwuf_get_follower_count( $user_id );
if ( $followed_count !== false ) {
$new_followed_count = update_user_meta( $user_id, '_pwuf_followed_by_count', $followed_count + 1 );
} else {
$new_followed_count = update_user_meta( $user_id, '_pwuf_followed_by_count', 1 );
}
do_action( 'pwuf_post_increase_followed_count', $user_id );
return $new_followed_count;
}
/**
* Decrease follower count
*
* Decrements the total count for how many users a specified user is followed by
*
* #access private
* #since 1.0
* #param int $user_id - the ID of the user to decrease the count for
* #return int
*/
function pwuf_decrease_followed_by_count( $user_id ) {
do_action( 'pwuf_pre_decrease_followed_count', $user_id );
$followed_count = pwuf_get_follower_count( $user_id );
if ( $followed_count ) {
$count = update_user_meta( $user_id, '_pwuf_followed_by_count', ( $followed_count - 1 ) );
do_action( 'pwuf_post_increase_followed_count', $user_id );
}
return $count;
}
/**
* Check if a user is following another
*
* Increments the total count for how many users a specified user is followed by
*
* #access private
* #since 1.0
* #param int $user_id - the ID of the user doing the following
* #param int $followed_user - the ID of the user to check if being followed by $user_id
* #return int
*/
function pwuf_is_following( $user_id, $followed_user ) {
$following = pwuf_get_following( $user_id );
$ret = false; // is not following by default
if ( is_array( $following ) && in_array( $followed_user, $following ) ) {
$ret = true; // is following
}
return $ret;
}
scripts.php
<?php
/**
* Loads plugin scripts
*
* #access private
* #since 1.0
* #return void
*/
function pwuf_load_scripts() {
wp_enqueue_script( 'pwuf-follow', PWUF_FOLLOW_URL . 'js/follow.js', array( 'jquery' ) );
wp_localize_script( 'pwuf-follow', 'pwuf_vars', array(
'processing_error' => __( 'There was a problem processing your request.', 'pwuf' ),
'login_required' => __( 'Oops, you must be logged-in to follow users.', 'pwuf' ),
'logged_in' => is_user_logged_in() ? 'true' : 'false',
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'follow_nonce' )
) );
}
add_action( 'wp_enqueue_scripts', 'pwuf_load_scripts' );

wooCommerce custom end points

I am a fairly new PHP developer and what I am trying to create is a custom endpoint for the "myaccount" page . I am trying to create a "Add Guest" end point. I am finding it very difficult to find any documentation online. What I have done so far is used this git hub repo
https://gist.github.com/neilgee/13ac00c86c903c4ab30544b2b76c483c/a43701564ab696e1586e2879591c890b67a5f1bf#file-woo-endpoints-order-php
I created these files and put them in the plugins/woocommerce/includes directory. But they seem to take no effect. Have i put them in the correct directory? Am I soppose to call these classes somewhere else ? I have no idea where I going wrong. Can some please educate me on this matter.
<?php
/*
* Add custom endpoint that appears in My Account Page - WooCommerce 2.6
* Ref - https://gist.github.com/claudiosmweb/a79f4e3992ae96cb821d3b357834a005#file-custom-my-account-endpoint-php
*/
class My_Custom_My_Account_Endpoint {
/**
* Custom endpoint name.
*
* #var add_students_details
*/
public static $endpoint = 'add_students_details';
/**
* Plugin actions.
*/
public function __construct() {
// Actions used to insert a new endpoint in the WordPress.
add_action( 'init', array( $this, 'add_endpoints' ) );
add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
// Change the My Accout page title.
add_filter( 'the_title', array( $this, 'endpoint_title' ) );
// Insering your new tab/page into the My Account page.
add_filter( 'woocommerce_account_menu_items', array( $this, 'new_menu_items' ) );
add_action( 'woocommerce_account_' . self::$endpoint . '_endpoint', array( $this, 'endpoint_content' ) );
}
/**
* Register new endpoint to use inside My Account page.
*
* #see https://developer.wordpress.org/reference/functions/add_rewrite_endpoint/
*/
public function add_endpoints() {
add_rewrite_endpoint( self::$endpoint, EP_ROOT | EP_PAGES );
}
/**
* Add new query var.
*
* #param array $vars
* #return array
*/
public function add_query_vars( $vars ) {
$vars[] = self::$endpoint;
return $vars;
}
/**
* Set endpoint title.
*
* #param string $title
* #return string
*/
public function endpoint_title( $title ) {
global $wp_query;
$is_endpoint = isset( $wp_query->query_vars[ self::$endpoint ] );
if ( $is_endpoint && ! is_admin() && is_main_query() && in_the_loop() && is_account_page() ) {
// New page title.
$title = __( 'My Stuff', 'woocommerce' );
remove_filter( 'the_title', array( $this, 'endpoint_title' ) );
}
return $title;
}
/**
* Insert the new endpoint into the My Account menu.
*
* #param array $items
* #return array
*/
public function new_menu_items( $items ) {
// Remove the logout menu item.
$logout = $items['customer-logout'];
unset( $items['customer-logout'] );
// Insert your custom endpoint.
$items[ self::$endpoint ] = __( 'My Stuff', 'woocommerce' );
// Insert back the logout item.
$items['customer-logout'] = $logout;
return $items;
}
/**
* Endpoint HTML content.
*/
public function endpoint_content() {
wc_get_template( 'myaccount/navigation.php' ); ?>
<div class="woocommerce-MyAccount-content">
<p>Hello World! - custom field can go here</p>
</div>
<?php
}
/**
* Plugin install action.
* Flush rewrite rules to make our custom endpoint available.
*/
public static function install() {
flush_rewrite_rules();
}
}
new My_Custom_My_Account_Endpoint();
// Flush rewrite rules on plugin activation.
register_activation_hook( __FILE__, array( 'My_Custom_My_Account_Endpoint', 'install' ) );
But they seem to take no effect. Have i put them in the correct directory? Am I soppose to call these classes somewhere else ? I have no idea where I going wrong. Can some please educate me on this matter.
First thing, you created a class, but you never loaded that file or initiated the class. The best way to do this would be in your own plugin.
Second, you have to add the add_rewrite_endpoint() to the install function. Otherwise, it doesn't know to register the new endpoint and your rewrite rules are flushed, but end up exactly the same as they were before... which creates some 404 errors.
Third, recent WooCommerce provides a filter for the endpoint title. And the content doesn't need to reproduce the My Account div or navigation.
Tested and working:
<?php
/**
* Plugin Name: WC Custom Endpoint
* Plugin URI: http://stackoverflow.com/questions/38784599/woocommerce-custom-end-points
* Description: A custom endpoint
* Version: 0.1.0
* Author: Kathy Darling
* Author URI: http://kathyisawesome.com
* Text Domain: wc_custom_endpoint
* Domain Path: /languages
* Requires at least: 4.6.0
* Tested up to: 4.6.0
*
* Copyright: © 2016 Kathy Darling.
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
/**
* The Main WC_Custom_Endpoint class
**/
if ( ! class_exists( 'WC_Custom_Endpoint' ) ) :
class WC_Custom_Endpoint {
const VERSION = '0.1.0';
/**
* Custom endpoint name.
*/
public static $endpoint = 'add_students_details';
/**
* #var WC_Custom_Endpoint - the single instance of the class
* #since 0.1.0
*/
protected static $instance = null;
/**
* Plugin Directory
*
* #since 0.1.0
* #var string $dir
*/
public $dir = '';
/**
* Plugin URL
*
* #since 0.1.0
* #var string $url
*/
public $url = '';
/**
* Main WC_Custom_Endpoint Instance
*
* Ensures only one instance of WC_Custom_Endpoint is loaded or can be loaded.
*
* #static
* #see WC_Custom_Endpoint()
* #return WC_Custom_Endpoint - Main instance
* #since 0.1.0
*/
public static function instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof WC_Custom_Endpoint ) ) {
self::$instance = new WC_Custom_Endpoint();
}
return self::$instance;
}
public function __construct(){
$this->dir = plugin_dir_path(__FILE__);
$this->url = plugin_dir_url(__FILE__);
// Load translation files
add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) );
// Actions used to insert a new endpoint in the WordPress.
add_action( 'init', array( $this, 'add_endpoints' ) );
add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
// Insering your new tab/page into the My Account page.
add_filter( 'woocommerce_account_menu_items', array( $this, 'new_menu_items' ) );
add_action( 'woocommerce_endpoint_' . self::$endpoint . '_title', array( $this, 'endpoint_title' ) );
add_action( 'woocommerce_account_' . self::$endpoint . '_endpoint', array( $this, 'endpoint_content' ) );
}
/*-----------------------------------------------------------------------------------*/
/* Localization */
/*-----------------------------------------------------------------------------------*/
/**
* Make the plugin translation ready
*
* #return void
* #since 1.0
*/
public function load_plugin_textdomain() {
load_plugin_textdomain( 'wc-custom-endpoint' , false , dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/*-----------------------------------------------------------------------------------*/
/* Endpoint */
/*-----------------------------------------------------------------------------------*/
/**
* Register new endpoint to use inside My Account page.
*
* #see https://developer.wordpress.org/reference/functions/add_rewrite_endpoint/
*/
public function add_endpoints() {
add_rewrite_endpoint( self::$endpoint, EP_ROOT | EP_PAGES );
}
/**
* Add new query var.
*
* #param array $vars
* #return array
*/
public function add_query_vars( $vars ) {
$vars[] = self::$endpoint;
return $vars;
}
/*-----------------------------------------------------------------------------------*/
/* Display */
/*-----------------------------------------------------------------------------------*/
/**
* Set endpoint title.
*
* #return string
*/
public function endpoint_title() {
return __( 'My Stuff', 'wc_custom_endpoint' );
}
/**
* Insert the new endpoint into the My Account menu.
*
* #param array $items
* #return array
*/
public function new_menu_items( $items ) {
// Remove the logout menu item.
$logout = $items['customer-logout'];
unset( $items['customer-logout'] );
// Insert your custom endpoint.
$items[ self::$endpoint ] = __( 'My Stuff', 'wc_custom_endpoint' );
// Insert back the logout item.
$items['customer-logout'] = $logout;
return $items;
}
/**
* Endpoint HTML content.
*/
public function endpoint_content() { ?>
<p>Hello World! - custom wc_get_template() can go here</p>
<?php
}
/*-----------------------------------------------------------------------------------*/
/* Activation */
/*-----------------------------------------------------------------------------------*/
/**
* Plugin install action.
* Flush rewrite rules to make our custom endpoint available.
*/
public static function install() {
WC_Custom_Endpoint()->add_endpoints();
flush_rewrite_rules();
}
/**
* Plugin install action.
* Flush rewrite rules to make our custom endpoint available.
*/
public static function uninstall() {
flush_rewrite_rules();
}
} //end class: do not remove or there will be no more guacamole for you
endif; // end class_exists check
/**
* Returns the main instance of WC_Custom_Endpoint to prevent the need to use globals.
*
* #since 1.0
* #return WC_Custom_Endpoint
*/
function WC_Custom_Endpoint() {
return WC_Custom_Endpoint::instance();
}
// Launch the whole plugin
add_action( 'woocommerce_loaded', 'WC_Custom_Endpoint' );
// register activation hook
register_activation_hook( __FILE__, array( 'WC_Custom_Endpoint', 'install' ) );
register_deactivation_hook( __FILE__, array( 'WC_Custom_Endpoint', 'uninstall' ) );

Add Shipping Email On Woocommerce Checkout And Send Email Notification

I want to add a extra email filed on woocommerce checkout page in the shipping area and send a order copy to that email, is it possible to do?
Was trying for quit long time to solve this but couldn't find a solution. Hope anyone can help me on this
Ah yes, I had forgotten that I posted that. Here is the complete, updated plugin. Newer versions of WooCommerce require the email recipients to be a comma-separated spring. The old version of my plugin was returning an array, which WooCommerce could not process.
<?php
/*
Plugin Name: WooCommerce Shipping Email
Plugin URI: https://gist.github.com/helgatheviking/d2975aa4d190a5b55922#
Description: Add a shipping email field to checkout and notify of new orders
Version: 1.0.1
Author: Kathy Darling
Author URI: http://kathyisawesome.com
Requires at least: 4.0
Tested up to: 4.0
Copyright: © 2014 Kathy Darling.
License: GNU General Public License v3.0
License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
/**
* The Main WC_Shipping_Email class
**/
if ( ! class_exists( 'WC_Shipping_Email' ) ) :
class WC_Shipping_Email {
/**
* #var WC_Shipping_Email - the single instance of the class
* #since 1.0
*/
protected static $_instance = null;
/**
* Main WC_Shipping_Email Instance
*
* Ensures only one instance of WC_Shipping_Email is loaded or can be loaded.
*
* #static
* #see WC_Shipping_Email()
* #return WC_Shipping_Email - Main instance
* #since 1.0
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Cloning is forbidden.
*
* #since 1.0
*/
public function __clone() {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce-mix-and-match' ), '2.0' );
}
/**
* Unserializing instances of this class is forbidden.
*
* #since 1.0
*/
public function __wakeup() {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'mix-and-match' ), '2.0' );
}
/**
* WC_Shipping_Email Constructor
*
* #access public
* #return WC_Shipping_Email
* #since 1.0
*/
public function __construct() {
$this->id = 'email';
$this->meta = '_shipping_email';
$this->label = __( 'Shipping Email', 'woocommerce-shipping-email' );
// add email field to checkout
add_filter( 'woocommerce_shipping_fields' , array( $this, 'add_shipping_fields' ) );
add_filter( 'woocommerce_admin_shipping_fields' , array( $this, 'admin_shipping_fields' ) );
// add recipient to specific emails
add_filter( 'woocommerce_email_recipient_customer_processing_order' , array( $this, 'add_recipient' ), 20, 2 );
add_filter( 'woocommerce_email_recipient_customer_completed_order' , array( $this, 'add_recipient' ), 20, 2 );
add_filter( 'woocommerce_email_recipient_customer_note' , array( $this, 'add_recipient' ), 20, 2 );
// display meta key in order overview
add_action( 'woocommerce_order_details_after_customer_details' , array( $this, 'after_customer_details' ) );
// display meta key in email
add_action( 'woocommerce_before_template_part' , array( $this, 'before_email_addresses' ), 10, 4 );
}
/*-----------------------------------------------------------------------------------*/
/* Plugin Functions */
/*-----------------------------------------------------------------------------------*/
/**
* Add email to front-end shipping fields
*
* #var array $fields
* #return array
* #since 1.0
*/
function add_shipping_fields( $fields ) {
$fields['shipping_' . $this->id] = array(
'label' => $this->label,
'required' => true,
'class' => array( 'form-row-first' ),
'validate' => array( 'email' ),
);
return $fields;
}
/**
* Add email to Admin Order overview
*
* #var array $fields
* #return array
* #since 1.0
*/
function admin_shipping_fields( $fields ) {
$fields[$this->id] = array(
'label' => $this->label
);
return $fields;
}
/**
* Add recipient to emails
*
* #var string $email
* #return string
* #since 1.0
*/
function add_recipient( $email, $order ) {
$additional_email = get_post_meta( $order->id, $this->meta, true );
if( $additional_email && is_email( $additional_email )){
if( is_array( $email ) ){
$email = explode( ',', $email );
array_push( $email, $additional_email );
$email = implode( ',', $email );
} elseif( is_string( $email ) ){
$email .= "," . $additional_email;
}
}
return $email;
}
/**
* Display meta in my-account area Order overview
*
* #var object $order
* #return null
* #since 1.0
*/
public function after_customer_details( $order ){
$value = get_post_meta( $order->id, $this->meta, true );
if( $value ){
echo '<dt>' . $this->label . ':</dt><dd>' . $value . '</dd>';
}
}
/**
* Display meta in my-account area Order overview
*
* #var array $fields
* #return array
* #since 1.0
*/
public function before_email_addresses( $template_name, $template_path, $located, $args ){
if( $template_name == 'emails/email-addresses.php' && isset( $args['order' ] ) && ( $value = get_post_meta( $args['order']->id, $this->meta, true ) ) ){
if ( isset( $args['plain_text'] ) && $args['plain_text'] ){
echo $this->label . ': ' . $value . "\n";
} else {
echo '<p><strong>' . $this->label . ':</strong> ' . $value . '</p>';
}
}
}
} //end class: do not remove or there will be no more guacamole for you
endif; // end class_exists check
/**
* Returns the main instance of WC_Shipping_Email to prevent the need to use globals.
*
* #since 2.0
* #return WooCommerce
*/
function WC_Shipping_Email() {
return WC_Shipping_Email::instance();
}
// Launch the whole plugin
WC_Shipping_Email();
NB: This only sends to the shipping email for the customer_processing_order, customer_completed_order, and customer_note emails.

Woocommerce: save_post hook is not being triggered

I have a meta box I'm coding for the product post type that comes with woocommerce. I've run into a problem I can't pass in that the 'save_post' hook does not seem to be working at all with products. It works perfectly for posts, but since I've changed my code to work for products it does nothing. The save_post function I've hooked to does absolutely nothing at the moment. I've added all sorts of code to it and it doesn't matter, the script just doesn't seem to get that far. am I missing something obvious?
Edit: as an aside, I added
?> <script type="text/javascript">
var post_id = '<?php $post_id ?>';
console.log("id is: " + post_id );
</script><?php
But it returns absolutely nothing.
<?php
/*
* Represents the plugin's Meta Box
*
* #since 0.0.1
* #package BBPlugin
* #subpackage BBPlugin
* #author Christopher Dando <captaindando#gmail.com>
*/
/*
* Represents the plugin's Meta Box
*
* Register's the meta box with the WordPress API, sets its properties,
* by including the markup from its associated view
*
* #package BBPlugin
* #subpackage BBPlugin/admin
* #author Christopher Dando <captaindando#gmail.com>
*/
class BBPlugin_Meta_Box{
/*
* Register this class with the wordpress API
*
* #since 0.0.1
*/
public function initialize_hooks(){
//add_action( 'add_meta_boxes_product', array( $this, 'add_meta_box' ) );
add_action( 'add_meta_boxes', array( $this, 'BBadd_meta_box' ) );
// This checks when wordpress is saving or
// updating a post.
add_action( 'save_post', array( $this, 'save_post' ) );
$junk = $post_id;
?> <script type="text/javascript">
var post_id = '<?php global $post; echo $post->ID; ?>';
console.log("id is: " + post_id );
</script><?php
}
// add_meta_boxes is the wordpress function. add_meta_box is our new function
/*
* The function responsible for creating the actual meta box.
*
* #since 0.0.1
*/
public function BBadd_meta_box(){
?> <script>console.log("meta box added");</script><?php
add_meta_box(
'BBPlugin',
"Brave Books",
array( $this, 'display_meta_box' ),
'product',
'normal',
'default'
);
}
// This defines the properties of the meta box.
/*
* Renders the content of the meta box.
*
* #since 0.0.1
*/
public function display_meta_box(){
include_once( 'views/BBPlugin-navigation.php' );
}
/**
* Sanitizes and serializes the information associated with this post.
*
* #since 0.0.1
*
* #param int $post_id The ID of the post that's currently being edited.
*/
// strangely, this calls if the meta box does not render
public function save_post( $post_id ) {
?><script>alert("post saved");</script><?php
/* If we're not working with a 'product' post type or the
user doesn't have permission to save,
then we exit the function.
*/
if ( ! $this->user_can_save( $post_id, 'BBPlugin_nonce', 'BBPlugin_save' ) ) {
return;
}
/*
We need to 'Sanitise' our information before
we can save it to the database. What this means
is that we must strip it of html tags
and extract the text itself.
*/
// If the 'Resources' inputs exist, iterate through them and sanitize them
if ($this->value_exists( 'BBPlugin-resources' ) ) {
// This is all divs with the id of meta-box-resources
$this->update_post_meta(
$post_id,
'BBPlugin-resources',
$this->sanitize_data( 'BBPlugin-resources', true )
);
}
else {
// leaving an input blank on the front end will remove that specific input.
$this->delete_post_meta( $post_id, 'BBPlugin-resources' );
}
}
/**
* Determines whether or not a value exists in the $_POST collection
* identified by the specified key.
*
* #since 0.0.1
*
* #param string $key The key of the value in the $_POST collection.
* #return bool True if the value exists; otherwise, false.
*/
private function value_exists( $key ) {
return ! empty( $_POST[ $key ] );
}
/**
* Deletes the specified meta data associated with the specified post ID
* based on the incoming key.
*
* #since 0.0.1
* #access private
* #param int $post_id The ID of the post containing the meta data
* #param string $meta_key The ID of the meta data value
*/
private function delete_post_meta( $post_id, $meta_key ) {
if ( '' !== get_post_meta( $post_id, $meta_key, true ) ) {
delete_post_meta( $post_id, '$meta_key' );
}
}
private function update_post_meta( $post_id, $meta_key, $meta_value ) {
if ( is_array( $_POST[ $meta_key ] ) ) {
$meta_value = array_filter( $_POST[ $meta_key ] );
}
/*
Update_post_meta also adds to a database if there is nothing there already.
parameters are as follows:
1. The post ID used to associate this information with the post.
2. A meta key that's used to uniquely identify the value.
3. The actual value associated with the meta key.
*/
update_post_meta( $post_id, $meta_key, $meta_value );
}
/**
* Sanitizes the data in the $_POST collection identified by the specified key
* based on whether or not the data is text or is an array.
*
* #since 1.0.0
* #access private
* #param string $key The key used to retrieve the data from the $_POST collection.
* #param bool $is_array Optional. True if the incoming data is an array.
* #return array|string The sanitized data.
*/
private function sanitize_data( $key, $is_array = false ) {
$sanitized_data = null;
if ( $is_array ) {
$resources = $_POST[ $key ];
$sanitized_data = array();
foreach ( $resources as $resource ) {
$resource = esc_url( strip_tags( $resource ) );
if ( ! empty( $resource ) ) {
$sanitized_data[] = $resource;
}
}
}
else {
$sanitized_data = '';
$sanitized_data = trim( $_POST[ $key ] );
$sanitized_data = esc_textarea( strip_tags( $sanitized_data ) );
}
return $sanitized_data;
}
/**
* Verifies that the post type that's being saved is actually a post (versus a page or another
* custom post type.
*
*
* #since 0.0.1
* #access private
* #return bool Return if the current post type is a post; false, otherwise.
*/
private function is_valid_post_type() {
return ! empty( $_POST['post_type'] ) && 'post' == $_POST['post_type'];
}
/**
* Determines whether or not the current user has the ability to save meta data associated with this post.
*
* #since 0.0.1
* #access private
* #param int $post_id The ID of the post being save
* #param string $nonce_action The name of the action associated with the nonce.
* #param string $nonce_id The ID of the nonce field.
* #return bool Whether or not the user has the ability to save this post.
*/
private function user_can_save( $post_id, $nonce_action, $nonce_id ) {
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ $nonce_action ] ) && wp_verify_nonce( $_POST[ $nonce_action ], $nonce_id ) );
// Return true if the user is able to save; otherwise, false.
return ! ( $is_autosave || $is_revision ) && $this->is_valid_post_type() && $is_valid_nonce;
}
}
?>
In Wordpress, save_post isn't a destination in itself; it is an action carried out effectively 'between' pages: you hit Update, and Wordpress will carry out a series of actions behind the scenes before returning you to the appropriate page (invariably the post you were just editing, with a notification about the status of that save).
As such, you will never see the results of an echo, print_r, or JS alert or console.log, because save_post isn't a user-facing action.
If you want to see if your save_post action is being carried out in this way, I would recommend throwing in a die(), like so:
public function save_post($post_id) {
?><script>alert("post saved");</script><?php
die();
}
If the save_post action is being fired correctly, then you should see the JS alert over a blank page. If you want to see if your function is carrying out any actual Wordpress-style functionality, I'd recommend a simple update_post_meta to confirm:
public function save_post($post_id) {
// Insert some actual logic to ensure you're not doing this on every post all the time
update_post_meta($post_id, 'i_am_saved', 'totes saved to post #' . $post_id);
}
You can then check the database (or click to view the custom fields within that post) to see if your custom metadata has been added.
I would also recommend attaching your save_post action specifically to the product post type that's used by WooCommerce:
add_action('save_post_product', array($this, 'save_post'));
That will save some redundancy checking later.

Categories