removing action in wordpress where action has an array with $this - php

I'm trying to simply replace a message that is added by a plugin called 'sensei' from woothemes.
For that my plan was to remove the action that places the message, and then add an action where i copied the normal function and edited to what i wanted it to be.
However the message was never removed and instead my new message is placed on the page next to the default message.
i used this code in functions.php:
add_action( 'sensei_loop_course_inside_before', 'remove_old_course_message', 15 );
add_action( 'sensei_loop_course_inside_before', 'active_no_course_message_output2', 11 );
function remove_old_course_message(){
global $Sensei_Shortcode_User_Courses;
remove_action( 'sensei_loop_course_inside_before', array( 'Sensei_Shortcode_User_Courses', 'sensei_loop_course_inside_before' ), 10);
remove_action( 'sensei_loop_course_inside_before', array( $Sensei_Shortcode_User_Courses, 'sensei_loop_course_inside_before' ), 10);
}
function active_no_course_message_output2(){
?>
<li class="user-active">
<div class="sensei-message info">
This is the message to be shown:
<?php _e( 'You have no active courses.', 'woothemes-sensei' ); ?>
<a href="www.google.com">
<?php _e( 'Start a Course!', 'woothemes-sensei' ); ?>
</a>
</div>
</li>
<?php
}
(all that changed is that i changed the button in the message to link to google instead of another page, this is so the code is a bit more understandable.)
In the plugin file there is a class named 'sensei shortcode-user-courses' and it contains the code:
if( empty( $active_ids ) ){
add_action( 'sensei_loop_course_inside_before', array( $this, 'active_no_course_message_output' ) );
}

Related

Custom Wordpress Plugin Not Saving Settings

I'm working on my first custom WP plugin and I'm trying to register a setting using the Settings API. I've followed a number of guides, and I've successfully created an admin menu page with the section and field defined in the plugin, but the single setting (in the code its fbm_lockout_updates) will not save, and no table in the database has been created for it.
EDIT: It is in fact in the options table. I was mistaken about that much. However, my options page still cant seem to update it, so I suppose my callback function is bad?
After trying a dozen things, I'm not sure where I'm going wrong. Here is the relevant code edited for brevity:
/* Create Menu */
add_action( 'admin_menu', 'fbm_config_menu' );
function fbm_config_menu() {
$page_title = 'Sample Plugin';
$menu_title = 'Sample Plugin Config';
$capability = 'manage_options';
$menu_slug = 'fbm_config';
$function = 'fbm_config_page';
add_menu_page(
$page_title,
$menu_title,
$capability,
$menu_slug,
$function
);
}
/* Register Settings and Fields */
function fbm_register_settings() {
register_setting( 'fbm_config', 'fbm_lockout_updates');
add_settings_section(
'fbm_restriction_section',
'Development Restrictions',
'fbm_restriction_callback',
'fbm_config'
);
add_settings_field(
'fbm_lockout_updates_field',
'Lockout Updates',
'fbm_lockout_field_callback',
'fbm_config',
'fbm_restriction_section'
);
}
add_action( 'admin_init', 'fbm_register_settings' );
/* Settings Callbacks */
function fbm_restriction_callback() {
?>
<p><?php esc_html_e( 'Description of Setting Section', 'fbm_config' ); ?></p>
<?php
}
function fbm_lockout_field_callback() {
$setting = get_option('fbm_lockout_updates');
?>
<input type="checkbox" name="fbm_lockout_updates" value="0" <?php checked('1', $setting); ?> >
<?php
}
/* Load Admin Page */
function fbm_config_page(){
if ( isset( $_GET['settings-updated'] ) ) {
add_settings_error( 'fbm_con_messages', 'fbm_con_message', __( 'Settings Saved', 'fbm_config' ), 'updated' );
}
settings_errors( 'fbm_con_messages' );
?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<form method="post" action="options.php">
<?php
settings_fields( 'fbm_config' );
do_settings_sections( 'fbm_config' );
submit_button( 'Save Settings' );
?>
</form>
<div>
<?php }
?>
In this case register_setting will save your data in wp-options table, take a look into it.
Recommended if you want your setting to be shown in the wordpress api use both admin and api hook
add_action( 'rest_api_init', 'bm_register_settings' );
add_action( 'admin_init', 'fbm_register_settings' );

Get and display product custom field on WooCommerce archive pages

I have successfully added a custom Metabox to admin product pages. The above code generate an Input type text in the Admin-Area where I can insert the city name:
add_action( 'save_post_product', 'set_post_metas_city', 10, 3 );
function set_post_metas_city( $post_id, $post, $update ) {
if(isset($_POST['city']))
update_post_meta($post_id, 'city', esc_attr($_POST['city']));
}
add_action( 'add_meta_boxes', 'metas_boxes_city', 50 );
function metas_boxes_city(){
add_meta_box( "options-city", "City Name", 'edit_form_after_title_cidade', 'product', 'normal' );
}
function edit_form_after_title_city($post) { ?>
<div id="mdiv">
<input type="text" style="width: 100%" name="city" value="<?php echo($post ? get_post_meta($post->ID,'city',true) : ''); ?>" id="city" spellcheck="true" autocomplete="off" placeholder="<?php _e('Insert the city'); ?>">
</div>
<?php
}
Code is in the active theme's functions.php file.
Now I use the following the hooked function, trying to display on the Shop Page, the custom field value (the city name) as follows:
// define the woocommerce_after_shop_loop_item callback
function action_woocommerce_after_shop_loop_item( ) {
// make action magic happen here...
echo($post ? get_post_meta($post->ID,'city',true) : '');
};
// add the action
add_action( 'woocommerce_after_shop_loop_item', 'action_woocommerce_after_shop_loop_item', 10, 0 );
So I am trying to get something like:
[Product Picture]
**City**
Category
Cake Ananas
$4,00
[BUY BUTTON]
But it still doesn't shows nothing up.
If I use a static content inside the echo, I get the desired display on the Shop Page:
// define the woocommerce_after_shop_loop_item callback
function action_woocommerce_after_shop_loop_item( ) {
// make action magic happen here...
echo "Statictest";
};
But the correct variable is missing..
Another point is, I don't know in this case in which page to run the action.
// run the action
do_action( 'woocommerce_after_shop_loop_item' );
What am I doing wrong please? Could someone give a Help please?
The main problem was in your last function with $post->ID. Since WooCommerce 3, there is also a much better hook than save_post_product. Try the following revisited code instead:
// Add custom product meta box
add_action( 'add_meta_boxes', 'add_product_metas_box_city', 50 );
function add_product_metas_box_city(){
add_meta_box( "options-city", __("City Name"), 'product_metas_box_city_content_callback', 'product', 'normal', 'high' );
}
// custom product meta box
function product_metas_box_city_content_callback( $post ) {
$value = get_post_meta( $post->ID, 'city', true );
echo '<div id="mdiv">
<input type="text" style="width: 100%" name="city" value="'. $value .'" id="city" spellcheck="true" autocomplete="off" placeholder="'. __('Insert the city') .'">
</div>';
}
// Save city custom field value
add_action( 'woocommerce_admin_process_product_object', 'save_product_city_meta_value' );
function save_product_city_meta_value( $product ) {
if( isset($_POST['city']) ) {
$product->update_meta_data( 'city', sanitize_text_field( $_POST['city'] ) );
}
}
// Display city value on frontend products loop
add_action( 'woocommerce_after_shop_loop_item', 'action_woocommerce_after_shop_loop_item', 10, 0 );
function action_woocommerce_after_shop_loop_item() {
global $product;
$city = $product->get_meta('city');
if ( $city ) {
echo '<p class="city">'. $city .'</p>';
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Displaying an admin message on woocommerce_order_action hook

I used this code :
https://gist.github.com/bekarice/5233ed58c3a836064123b290463241c0
In sv_wc_process_order_meta_box_action function, how is possible to display a message box to admin?
Currently code uses update_post_meta() function and add_order_note() method and not display any message to admin.
Thanks.
The only way I know is to use custom functions with the admin_notices action hook. So you could try to include the related add_action() inside the code you are using.
This code is untested, and I don't guarantee anything:
// The message function to be hooked in 'admin_notices' hook.
function my_custom_admin_notice() {
?>
<div class="notice notice-success is-dismissible">
<p><?php _e('Order has been updated "printed for packaging"'); ?></p>
</div>
<?php
}
//The second function that you use (customized with an add_action()):
function sv_wc_process_order_meta_box_action( $order ) {
// add the order note
$message = sprintf( __( 'Order information printed by %s for packaging.', 'my-textdomain' ), wp_get_current_user()->display_name );
$order->add_order_note( $message );
// add the flag so this action won't be shown again
update_post_meta( $order->id, '_wc_order_marked_printed_for_packaging', 'yes' );
// Setting the admin message function in 'admin_notices' hook.
add_action('admin_notices', 'my_custom_admin_notice');
}
add_action( 'woocommerce_order_action_wc_custom_order_action', 'sv_wc_process_order_meta_box_action' );
Related documentation:
Complete Guide to WordPress Admin Notices
How to Add Admin Alerts and Error Messages to the Backend of WordPress

getting error in activation of theme in wordpress

I have install a new theme in wordpress after uploading but at the time of activation of the theme , getting the error like this:
Parse error: parse error in C:\wamp\www\wordpress\wp-content\themes\realhomes\framework\meta-box\inspiry-meta-box.php on line 11
Please help me through this:
add_action('admin_init',function(){ : is line 11 in my code.
add_action( 'admin_init', function() {
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
// Meta Box Plugin
if ( is_plugin_active( 'meta-box/meta-box.php' ) ) {
deactivate_plugins( 'meta-box/meta-box.php' );
add_action( 'admin_notices', function () {
?>
<div class="update-nag notice is-dismissible">
<p><strong><?php _e( 'Meta Box plugin has been deactivated!', 'framework' ); ?></strong></p>
<p><?php _e( 'As now its functionality is embedded with in Real Homes theme.', 'framework' ); ?></p>
<p><em><?php _e( 'So, You should completely remove it from your plugins.', 'framework' ); ?></em></p>
</div>
<?php
} );
}
please check your brackets not over in your last line of code..
please add this in last line });

customise endpoint urls in woocommerce

I'm trying to create a tabbed version of "My Account" in woocommerce.
I have built my pages in php using bootstrap css and included the wordpress header and footer so the page loads correctly.
The tabs display as expected by I am having an issue with the endpoint urls in woocommerce:
- 'edit-address'
- 'view-order'
These endpoints are generated dynamically and appended to the url: www.site-name.com/my-account/edit-address
Here's the function call (in file .plugins/woocommerce/myaccount/form-edit-address.php):
<a href="<?php echo wc_get_endpoint_url( 'edit-address', $name ); ?>
I've included 'my-addresses' in my tabbed page which dispays ok. However, the link to edit the shipping and billing addresses is generated by the above call to the endpoints. When the link is clicked the page fails to load and returns a 404 error.
My custom php page is basically www.site-name.com/account/edit-address
The problem is:
The 'edit-address' page content is not loading and I get a 404 error
I'm guessing the issue is caused because my pages are external php pages and not stored with wp database?
Is there a way I can customise the endpoint url so it appends correctly and loads the page?
Link to the page on my site: www.thecookerytutor dot co dot uk/account
Origional woocommerce my-account page: www.thecookerytutor dot co dot uk/my-account
(You will need to create a login to view both pages)
It's had me stumped for days!
Thanks in advance.
Code for my addresses tab on my custom php page as promised...
echo "<div id = 'edit-addresses' class='tab-pane fade'>";
echo "<div id = 'content' class = 'page col-full'>";
echo "<section id = 'main' class = 'col-left'>";
echo "<BR>";
include "my-address.php";
echo "</section>";
echo "</div>";
echo "</div>";`
As you can see I'm including the wc page that loads the addresses (I've copied to the local folder)
Here's the content of edit-address.php (I've copied to my local folder and added wp header). The page loads the header but the get address function fails at line 50.
<?php
define('WP_USE_THEMES', false);
require('../wp-blog-header.php');
add_filter( 'wp_title', 'wp_title_so_18381106', 10, 3 );
function wp_title_so_18381106( $title, $sep, $seplocation )
{
return 'Your Account | ';
}
get_header();
?>
<?php
/**
* Edit address form
*
* #author WooThemes
* #package WooCommerce/Templates
* #version 2.1.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $current_user;
$page_title = ( $load_address === 'billing' ) ? __( 'Billing Address', 'woocommerce' ) : __( 'Shipping Address', 'woocommerce' );
get_currentuserinfo();
?>
<?php wc_print_notices(); ?>
<?php //if ( ! $load_address ) : ?>
<?php //wc_get_template( 'myaccount/my-address.php' ); ?>
<?php //else : ?>
<form method="post">
<h3><?php echo apply_filters( 'woocommerce_my_account_edit_address_title', $page_title ); ?></h3>
<?php do_action( "woocommerce_before_edit_address_form_{$load_address}" ); ?>
/fails to load here/
<?php foreach ( $address as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, ! empty( $_POST[ $key ] ) ? wc_clean( $_POST[ $key ] ) : $field['value'] ); ?>
<?php endforeach; ?>
<?php do_action( "woocommerce_after_edit_address_form_{$load_address}" ); ?>
<p>
<input type="submit" class="button" name="save_address" value="<?php _e( 'Save Address', 'woocommerce' ); ?>" />
<?php wp_nonce_field( 'woocommerce-edit_address' ); ?>
<input type="hidden" name="action" value="edit_address" />
</p>
</form>
<?php //endif; ?>
WooCommerce is a plugin for WordPress, you can't use it without WordPress... for example /my-account/edit-address/, that my-account is actually the page you are rendering and edit-address is just a variable. edit-address was added as an endpoint to my-account. you need to do the same for your account.
You should get an idea of what endpoints are.
Other thoughts about this. If you really want to use account and not my-account, look into your pages for "My Account" and change it's permalink. You can also create another page that can use account in it's permalink. Then use that page in your WooCommerce > Settings > Account tab > My Account Page.
If you have some custom php codes, you can just copy the template from your woocommerce plugin to your theme folder and edit the file you need. Read Template Structure + Overriding Templates via a Theme.
And if I have not addressed your problem, comment down below.

Categories