How am I supposed to call a secondary PHP file? Here is my code.
add_filter( 'woocommerce_product_tabs', 'woo_simfree_product_tab' );
function woo_simfree_product_tab( $tabs ) {
global $post;
if( function_exists('get_product') ){
$product = get_product( $post->ID );
if( $product->is_type( 'grouped' ) ){
$tabs['simfree-plans'] = array( 'title' => __( 'SIM Free', 'woocommerce' ), 'priority' => 20, 'callback' => 'woo_simfree_product_tab_content' );
return $tabs;
} else {
return $tabs;
}
}
}
function woo_simfree_product_tab_content() {
require get_template_directory() . "/custom-groups/grouped-simfree.php";
}
The problem is fetching the file right here... (3rd line from the bottom)
require get_template_directory() . "/custom-groups/grouped-simfree.php";
This does not work and causes strange behaviour. I have a custom PHP file I want to load in this tab I have created (grouped-simfree.php) but I don't know how to make it run.
What is the correct way to load a custom PHP file in wordpress from a function on a hook?
EDIT: (What's wrong with this picture?) I actually solved this problem years ago but now I've come back to the same problem but the same solution is not working for some reason. source (my question from 2014): https://stackoverflow.com/questions/30233440/woocommerce-woocommerce-grouped-add-to-cart-function
function woocommerce_grouped_add_to_cart2() {
global $product;
wc_get_template( get_template_directory() . '/custom-groups/grouped-simfree.php', array(
'grouped_product' => $product,
'grouped_products' => $product->get_children(),
'quantites_required' => false
) );
}
function woo_simfree_product_tab_content() {
woocommerce_grouped_add_to_cart2();
}
EDIT 2
If I move the custom template into the woocommerce plugin templates folder.
#Reigel this works but now im gonna lose the template when ever I update woocommerce I just realised this is what I did a couple years ago and now I realise why my site crashed because the templates were overwritten during a woocommerce update
function woocommerce_grouped_add_to_cart2() {
global $product;
wc_get_template( 'single-product/add-to-cart/grouped-simfree.php', array(
'grouped_product' => $product,
'grouped_products' => $product->get_children(),
'quantites_required' => false
) );
}
function woo_simfree_product_tab_content() {
woocommerce_grouped_add_to_cart2();
}
You'll need to use get_stylesheet_directory() to include your file, if it's a child theme do something like this:
require get_stylesheet_directory() . "/custom-groups/grouped-simfree.php";
The file should be at wp-content/themes/your-child-theme/custom-groups/grouped-simfree.php
Related
I have the following folder structure for my theme:
theme
inc
theme
functions.php
init.php
functions.php
In inc/theme/functions.php, I'm placing all theme specific functions (i.e. removing taxonomies etc). In theme/functions.php, I have all my core functions.
With my current code, WordPress states "The site is experiencing technical difficulties.". If I delete everything in theme/functions.php, the content loads, but the code in inc/theme/functions.php is not being executed. For example, in inc/theme/functions.php, I have wp_enqueue_style( 'style', get_stylesheet_uri() ); and none of the styles are pulling through.
Can't seem to figure out:
Why theme/functions.php is causing a WordPress error.
Why inc/theme/functions.php is not being executed.
theme/functions.php
<?php
require_once trailingslashit( get_template_directory() ) . 'inc/init.php';
new theme_ThemeFunctions;
class theme_ThemeFunctions {
function __construct() {
load_theme_textdomain( 'theme' );
add_action( 'init', array( $this, 'post_types_taxonomies' ) );
add_action( 'init', array( $this, 'register_menus' ) );
}
public function post_types_taxonomies() {
register_post_type(
'case-studies',
build_post_args(
'case-study', 'Case study', 'Case studies',
array(
'menu_icon' => 'dashicons-book',
'menu_position' => 20,
'has_archive' => true
)
)
);
}
public function register_menus() {
register_nav_menus(
array(
'main' => __( 'Main Menu', 'theme' ),
)
);
}
}
?>
inc/theme/functions.php
<?php
function scriptAndStyles() {
wp_enqueue_style( 'style', get_stylesheet_uri() );
}
add_action( 'wp_enqueue_scripts', 'scriptAndStyles' );
function remove_editor() {
remove_post_type_support('page', 'editor');
}
add_action('admin_init', 'remove_editor');
// Remove featured image option from pages
function remove_thumbnail_box() {
remove_meta_box( 'postimagediv','page', 'side' );
}
add_action('do_meta_boxes', 'remove_thumbnail_box');
// Remove posts type option
function post_remove () {
remove_menu_page('edit.php');
}
add_action('admin_menu', 'post_remove');
?>
inc/init.php
<?php
$include_dir = trailingslashit( get_template_directory() ) . 'inc/';
// Load any custom functions
require_once $include_dir . 'theme/functions.php';
?>
You need use get_template_directory() for require your files.
And use new theme_ThemeFunctions below class itself.
Also use to view errors:
define('WP_DEBUG', 1);
define('WP_DEBUG_DISPLAY', 1);
I have previously used a solution described here: remove_action From PHP Class for removing an action in the WooCommerce membership plugin.
However, the solution no longer works, as WooComemerce have changed the code behind the membership plugin.
So this is the new code.
Main woocommerce-memberships.php
public function includes() {
// load post types
require_once( $this->get_plugin_path() . '/includes/class-wc-memberships-post-types.php' );
// load user messages helper
require_once( $this->get_plugin_path() . '/includes/class-wc-memberships-user-messages.php' );
// load helper functions
require_once( $this->get_plugin_path() . '/includes/functions/wc-memberships-functions.php' );
// init general classes
$this->rules = $this->load_class( '/includes/class-wc-memberships-rules.php', 'WC_Memberships_Rules' );
$this->plans = $this->load_class( '/includes/class-wc-memberships-membership-plans.php', 'WC_Memberships_Membership_Plans' );
$this->emails = $this->load_class( '/includes/class-wc-memberships-emails.php', 'WC_Memberships_Emails' );
$this->user_memberships = $this->load_class( '/includes/class-wc-memberships-user-memberships.php', 'WC_Memberships_User_Memberships' );
$this->capabilities = $this->load_class( '/includes/class-wc-memberships-capabilities.php', 'WC_Memberships_Capabilities' );
$this->member_discounts = $this->load_class( '/includes/class-wc-memberships-member-discounts.php', 'WC_Memberships_Member_Discounts' );
$this->restrictions = $this->load_class( '/includes/class-wc-memberships-restrictions.php', 'WC_Memberships_Restrictions' );
Main instance
function wc_memberships() {
return WC_Memberships::instance();
}
From included class-wc-memberships-restrictions.php file
/**
* Returns the general content restrictions handler.
*
* #since 1.9.0
*
* #return null|\WC_Memberships_Posts_Restrictions
*/
public function get_posts_restrictions_instance() {
if ( ! $this->posts_restrictions instanceof WC_Memberships_Posts_Restrictions ) {
$this->posts_restrictions = wc_memberships()->load_class( '/includes/frontend/class-wc-memberships-posts-restrictions.php', 'WC_Memberships_Posts_Restrictions' );
}
return $this->posts_restrictions;
}
Then in class-wc-memberships-posts-restrictions.php
public function __construct() {
// decide whether attempting to access restricted content has to be redirected
add_action( 'wp', array( $this, 'handle_restriction_modes' ) );
// restrict the post by filtering the post object and replacing the content with a message and maybe excerpt
add_action( 'the_post', array( $this, 'restrict_post' ), 0 );
How do i remove the 'the_post' action?
So far i have the following in functions.php theme file:
function weteach_remove_actions(){
if(is_singular( 'post' )) {
if( function_exists( 'wc_memberships' ) ){
remove_action( 'the_post', array( wc_memberships()->restrictions, 'restrict_post' ));
}
}
return;
}
add_action( 'the_post', 'weteach_remove_actions', 1 );
Which gives me a "blank-page"-error.
Could you tell us what the error message was? My guess is that restrictions and post_restrictions aren't the same property and so you aren't finding the restrict_post method in the right class.
Edited now that I have looked at Memberships, this seems to work for me:
function so_41431558_remove_membership_post_restrictions(){
if( function_exists( 'wc_memberships' ) && version_compare( WC_Memberships::VERSION, '1.9.0', '>=' ) && is_singular( 'post' ) ){
remove_action( 'the_post', array( wc_memberships()->get_restrictions_instance()->get_posts_restrictions_instance(), 'restrict_post' ), 0 );
}
}
add_action( 'wp_head', 'so_41431558_remove_membership_post_restrictions', 1 );
Your add_action attempt is happening on priority 1, which is after the function has already run the Memberships method on priority 0, so even if the rest of your code was correct it would be too late.
So 1. I think we need to go to an earlier hook.
And 2. I think we need to use the new method for accessing the post restrictions class instance.
edited to add
and 3. I've switched to a direct version compare condition
and 4. I misread where the get_posts_restrictions_instance() method was... it is accessed via wc_memberships()->get_restrictions_instance()->get_posts_restrictions_instance()
I have a review plugin that overrides the comment form in a specific posttype.
Now I'm trying to seperate the reviews and comments.
My first step is to remove the filter that modifies the current comment template and use that filter inside a second comment form.
The plugin uses this code (simplified)
final class DM_Reviews {
public function hooks() {
do_action_ref_array( 'dm_reviews_before_setup_actions', array( &$this ) );
add_filter( 'comment_form_defaults', array( $this, 'reviews_form' ) );
do_action_ref_array( 'dm_reviews_after_setup_actions', array( &$this ) );
}
public function review_form( $args ) {
$form = 'plugin code to modify form';
return wp_parse_args( $form, $args );
}
}
In my child theme's function.php file, I tried to use this but it didn't worked.
global $DM_Reviews;
remove_filter( 'comment_form_defaults', array($DM_Reviews, 'reviews_form'),1 );
WP Codex
If someone can put me in the right direction on how to solve it, it would help me a lot.
I think you can achieve this goal, using one of the following solutions depending on the way this plugin instantiates the class:
if( class_exists('DM_Reviews' ) ){
//This should work in whatever case, not tested
remove_filter('comment_form_defaults', array( 'DM_Reviews', 'reviews_form'));
//or Instantiating a new instance, not tested
remove_filter('comment_form_defaults', array( new DM_Reviews(), 'reviews_form'));
//or Targeting the specific instance, not tested
remove_filter('comment_form_defaults', array( DM_Reviews::get_instance(), 'reviews_form'));
}
Hope it helps, let me know if you get stuck.
for me remove_filter didn't work from function.php i wanted to remove a specific behavior of a plugin so what i did :
add_action( 'init', 'remove_filters' );
function remove_filters(){
global $wp_filter;
unset( $wp_filter["_filter_name"]);
}
Try this :
$instance = DM_Reviews::this();
remove_filter('comment_form_defaults', array( $instance, 'reviews_form'));
I have plugin that create new post type. Also plugin set single template for it's single page.
add_filter( 'single_template', array( &$this, 'register_ipa_product_post_type_single_template' ) );
function register_ipa_product_post_type_single_template( $single_template ) {
global $post;
if ( $post->post_type == 'product' ) {
$single_template = IPA_PRODUCT_POST_TYPE_TEMPLATE_FOLDER . 'single-product.php';
}
return $single_template;
}
How i can override single-product.php in my theme.
I don't found any solutions for my question at this site.
just filter it a little later than the current function (ps if doing this within a class you need to reference it using array(&$this, 'function'). I left it out as i assume you are using the functions.php or function override....etc
add_filter( 'single_template', 'register_ipa_product_post_type_single_template', 100 );
function change_temp( $single_template ) {
global $post;
if ( $post->post_type == 'product' ) {
$single_template = 'path to your template file';
}
return $single_template;
};
I'm trying to use a widget within a plugin in wordpress and I'm seeing this error within the widget box:
Warning: extract() [function.extract]: First argument should be an array in /nfs/c03/h04/mnt/57957/domains/rab.qbessi.com/html/wp-content/plugins/register-plus/dash_widget.php on line 24
This is the code from Line 24:
// Output the widget contents
function widget( $args ) {
extract( $args, EXTR_SKIP );
Here's the dash_widget.php code
<?php
if( !class_exists('RegisterPlusWidget') ){
class RegisterPlusWidget{
function RegisterPlusWidget() { //contructor
// Add the widget to the dashboard
add_action( 'wp_dashboard_setup', array($this, 'register_widget') );
add_filter( 'wp_dashboard_widgets', array($this, 'add_widget') );
}
function register_widget() {
wp_register_sidebar_widget( 'regplus_invite_tracking', __( 'Invitation Code Tracking', 'regplus' ), array($this, 'widget'), array( 'settings' => 'options-general.php?page=register-plus' ) );
}
// Modifies the array of dashboard widgets and adds this plugin's
function add_widget( $widgets ) {
global $wp_registered_widgets;
if ( !isset($wp_registered_widgets['regplus_invite_tracking']) ) return $widgets;
array_splice( $widgets, 2, 0, 'regplus_invite_tracking' );
return $widgets;
}
// Output the widget contents
function widget( $args ) {
extract( $args, EXTR_SKIP );
echo $before_widget;
echo $before_title;
echo $widget_name;
echo $after_title;
global $wpdb;
$regplus = get_option( 'register_plus' );
$codes = $regplus['codepass'];
$usercodes = array();
foreach($codes as $code){
$users = $wpdb->get_results( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key='invite_code' AND meta_value='$code'" );
echo '<h3>' . $code . ': <small style="font-weight:normal">' . count($users) . ' Users Registered.</small></h3>';
}
echo $after_widget;
}
}
} # End Class RegisterPlusWidget
// Start this plugin once all other plugins are fully loaded
add_action( 'plugins_loaded', create_function( '', 'global $regplus_widget; $regplus_widget = new RegisterPlusWidget();' ) );
?>
The widget() function is being called with no parameters. Why, is hard to tell without digging deeply into the plugin. You should ask the plugin's author.
You can try adding
// Output the widget contents
function widget( $args ) {
if (is_array($args)) // Add this
extract( $args, EXTR_SKIP );
and see whether the output still makes sense then. That effectively just suppresses the action that causes the warning. If it's a badly programmed plugin that was developed with warnings turned off, that already may do the trick.
google launched new plugin called site kit, it will make analyzing work much more easier.
https://wordifact.com/google-site-kit/