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);
Related
I'm working on a project that I have a child-theme on which I'm running specific functions from the function.php file.
My problem is that when I try to add an if condition to only run the script on a specific page, it doesn't work.
I'm using the functions.php from the child theme.
In other words... I need to be able to get the current page on the child theme.
What am I doing wrong?
add_action(
'init', function() {
if (is_page('contact')) {
wp_register_script( 'my-func', get_stylesheet_directory_uri() . 'my-func.js', '', '', false );
wp_enqueue_script( 'my-func' );
}
}
);
Thanks
for theme code will go to your functions.php file and for plugin code will go to the public functions.
add_action( 'wp_enqueue_scripts', 'my_assets' );
function my_assets()
{
if (is_page('booking')) {
/* for plugin */
wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/custom.css', array(), $this->version, 'all');
/*for theme */
wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}
}
I am writing a custom WordPress plugin, I am using the OceanWP Theme with Elementor, and am trying to enqueue/register Bootstrap 4.5 styles/scripts as well as my own custom styles/scripts.
However, OceanWP's styles are still taking precedence being used instead of my styles/scripts.
Currently, I am trying to over ride the themes assets by upping the priority in the add_action hook but am not having any luck.
I am trying to display a custom multi-part form and display it on a page using a shortcode.
public function __construct()
{
//set dirpath
$this->_dirpath = dirname(__FILE__);
add_action('wp_enqueue_scripts', array($this, 'cmmc_styles'), 9999);
add_action('wp_footer', array($this, 'cmmc_scripts'));
add_shortcode("sme-cmmc-form", array($this, "displayCmmcForm"));
}
public function cmmc_scripts()
{
///wp_enqueue_style('bootstrap_css', 'https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css', false, NULL, 'all');
wp_enqueue_script('popper_js', 'https://cdn.jsdelivr.net/npm/popper.js#1.16.1/dist/umd/popper.min.js', array('jquery'), NULL, true);
wp_enqueue_script('bootstrap_js', 'https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js', array('jquery'), NULL, true);
wp_enqueue_script('cmmc_js', plugin_dir_url( __FILE__ ) . 'assets/js/app.js', array('jquery'), '1.0' );
//wp_enqueue_style('custom_styles', plugins_url('/assets/css/styles.css', __FILE__));
}
public function cmmc_styles() {
wp_register_style('bootstrap_css', 'https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css', false, NULL, 'all' );
wp_enqueue_style('bootstrap_css');
wp_enqueue_style('custom_styles', plugins_url('/assets/css/styles.css', __FILE__));
}
Can someone please tell me how I could possibly over ride the themes styles, even if it is just for this plugin, or dequeue the styles for this single page temporarily?
EDIT: to add in the enqueued styles and scripts from the theme
add_action( 'wp_enqueue_scripts', array( 'OCEANWP_Theme_Class', 'theme_css' ) );
// Load his file in last.
add_action( 'wp_enqueue_scripts', array( 'OCEANWP_Theme_Class', 'custom_style_css' ), 9999 );
// Remove Customizer CSS script from Front-end.
add_action( 'init', array( 'OCEANWP_Theme_Class', 'remove_customizer_custom_css' ) );
// Load theme js.
add_action( 'wp_enqueue_scripts', array( 'OCEANWP_Theme_Class', 'theme_js' ) );
/**
* Load front-end scripts
*
* #since 1.0.0
*/
public static function theme_css() {
// Define dir.
$dir = OCEANWP_CSS_DIR_URI;
$theme_version = OCEANWP_THEME_VERSION;
// Remove font awesome style from plugins.
wp_deregister_style( 'font-awesome' );
wp_deregister_style( 'fontawesome' );
// Load font awesome style.
wp_enqueue_style( 'font-awesome', OCEANWP_THEME_URI . '/assets/fonts/fontawesome/css/all.min.css', false, '5.11.2' );
// Register simple line icons style.
wp_enqueue_style( 'simple-line-icons', $dir . 'third/simple-line-icons.min.css', false, '2.4.0' );
// Register the lightbox style.
wp_enqueue_style( 'magnific-popup', $dir . 'third/magnific-popup.min.css', false, '1.0.0' );
// Register the slick style.
wp_enqueue_style( 'slick', $dir . 'third/slick.min.css', false, '1.6.0' );
// Main Style.css File.
wp_enqueue_style( 'oceanwp-style', $dir . 'style.min.css', false, $theme_version );
// Register hamburgers buttons to easily use them.
wp_register_style( 'oceanwp-hamburgers', $dir . 'third/hamburgers/hamburgers.min.css', false, $theme_version );
// Register hamburgers buttons styles.
$hamburgers = oceanwp_hamburgers_styles();
foreach ( $hamburgers as $class => $name ) {
wp_register_style( 'oceanwp-' . $class . '', $dir . 'third/hamburgers/types/' . $class . '.css', false, $theme_version );
}
// Get mobile menu icon style.
$mobileMenu = get_theme_mod( 'ocean_mobile_menu_open_hamburger', 'default' );
// Enqueue mobile menu icon style.
if ( ! empty( $mobileMenu ) && 'default' !== $mobileMenu ) {
wp_enqueue_style( 'oceanwp-hamburgers' );
wp_enqueue_style( 'oceanwp-' . $mobileMenu . '' );
}
// If Vertical header style.
if ( 'vertical' === oceanwp_header_style() ) {
wp_enqueue_style( 'oceanwp-hamburgers' );
wp_enqueue_style( 'oceanwp-spin' );
}
}
/**
* Returns all js needed for the front-end
*
* #since 1.0.0
*/
public static function theme_js() {
// Get js directory uri.
$dir = OCEANWP_JS_DIR_URI;
// Get current theme version.
$theme_version = OCEANWP_THEME_VERSION;
// Get localized array.
$localize_array = self::localize_array();
// Comment reply.
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
// Add images loaded.
wp_enqueue_script( 'imagesloaded' );
// Register nicescroll script to use it in some extensions.
wp_register_script( 'nicescroll', $dir . 'third/nicescroll.min.js', array( 'jquery' ), $theme_version, true );
// Enqueue nicescroll script if vertical header style.
if ( 'vertical' === oceanwp_header_style() ) {
wp_enqueue_script( 'nicescroll' );
}
// Register Infinite Scroll script.
wp_register_script( 'infinitescroll', $dir . 'third/infinitescroll.min.js', array( 'jquery' ), $theme_version, true );
// WooCommerce scripts.
if ( OCEANWP_WOOCOMMERCE_ACTIVE
&& 'yes' !== get_theme_mod( 'ocean_woo_remove_custom_features', 'no' ) ) {
wp_enqueue_script( 'oceanwp-woocommerce', $dir . 'third/woo/woo-scripts.min.js', array( 'jquery' ), $theme_version, true );
}
// Load the lightbox scripts.
wp_enqueue_script( 'magnific-popup', $dir . 'third/magnific-popup.min.js', array( 'jquery' ), $theme_version, true );
wp_enqueue_script( 'oceanwp-lightbox', $dir . 'third/lightbox.min.js', array( 'jquery' ), $theme_version, true );
// Load minified js.
wp_enqueue_script( 'oceanwp-main', $dir . 'main.min.js', array( 'jquery' ), $theme_version, true );
// Localize array.
wp_localize_script( 'oceanwp-main', 'oceanwpLocalize', $localize_array );
}
It's hard to say how to dequeue the scripts w/o having a look at the theme source code. Anyways usually you just need to wait until the theme has done it's job, hook into wp and them remove the styles searching for their handles name. Something like this:
add_action('after_setup_theme','alter_styles');
function alter_styles(){
wp_dequeue_style();
wp_dequeue_script();
}
Instead, speaking of your code, first question is: are you sure you load that code at the right time, in the right place or does it get executed at all?
You can do something like:
add_action('template_redirect','my_template_redirect');
function my_template_redirect(){
if (is_page('your_page')){
// Load class / do stuff with scripts/styles
}
}
to be sure and execute the code just for that particular page
If you're using the same css class names, in your css file just add !important before the semi-colon to the attributes you want to force.
There are a number of ways you can get your stylesheets to load after other stylesheets are loaded. You have already tried a few ways, but the parent theme is a very high priority of 9999 so you need to use an even higher one or it won't work.
1. Priority
You are using priority 9999 in your add_action, but if you look at the parent theme, it uses:
add_action( 'wp_enqueue_scripts', array( 'OCEANWP_Theme_Class', 'custom_style_css' ), 9999 );
The priority for your code isn't actually higher than the parent theme, so you need to go higher again, e.g.
add_action('wp_enqueue_scripts', array($this, 'cmmc_styles'), 10000);
2. Dequeuing (Note, you need to match the values used when it was enqueued)
You said dequeuing didn't work for you, but note that when you dequeue a style the priority determines when it runs - just like when you are enqueuing - so you need to use a higher priority than what was used when it was enqueued. It also must use the same hook (or a later one).
As we saw above, you need to do this with a priority higher than the 9999 they were enqueued with, e.g.
function dequeue_oceanwp_styles() {
wp_dequeue_style('oceanwp-style');
wp_deregister_style('oceanwp-style');
}
/* Now call this function with a higher priority than 9999 */
add_action( 'wp_enqueue_scripts', 'dequeue_oceanwp_styles', 10000);
3. Dependencies
If that doesn't work you can set up dependencies between the styles to force the order.
When you use either wp_register_style or wp_enqueue_style, you can specify dependencies for the stylesheet you are registering/enqueuing - i.e. the other stylesheets needed by your stylesheet. This means that the stylesheet you are registering won't get loaded until after the ones it depends on.
To do this, you pass an array of the handles for the stylesheets that must be loaded first, e.g.
// create an array with the handles of the stylesheets you want to load before yours
$dependencies = array('oceanwp-style', 'oceanwp-hamburgers', /* etc. */ );
/* Noew pass this in as the dependencies for your stylesheets */
wp_register_style('bootstrap_css',
'https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css',
$dependencies, /* array of dependencies */
NULL, 'all' );
wp_enqueue_style('bootstrap_css');
/* Add bootstrap to the dependencies, if your custom_styles needs it */
$dependencies[] = 'bootstrap_css';
wp_enqueue_style('custom_styles',
plugins_url('/assets/css/styles.css', __FILE__),
$dependencies, /* array of dependencies */
);
Any reference to googleapis will block my website in China. How can I remove these references?
Here are the results of my grep search for google api reference:
./codesearch.php:$command = "grep -ri 'ajax.googleapis.com' ./*"; ./wp-content/cache/all/privacy-policy/index.html: ./wp-content/cache/all/privacy-policy/index.html: ./wp-content/cache/all/contact/index.html: ./wp-content/cache/all/contact/index.html: ./wp-content/cache/all/index.html: ./wp-content/cache/all/index.html: ./wp-content/cache/all/author/boomerlanglearning/index.html: ./wp-content/cache/all/author/boomerlanglearning/index.html: ./wp-content/cache/all/my-account/index.html: ./wp-content/cache/all/my-account/index.html: ./wp-content/cache/all/my-account/lost-password/index.html: ./wp-content/cache/all/my-account/lost-password/index.html: ./wp-content/cache/all/terms-and-conditions/index.html: ./wp-content/cache/all/terms-and-conditions/index.html: ./wp-content/plugins/woocommerce-checkout-manager/woocommerce-checkout-manager.php: // wp_enqueue_script( 'jquery-lib', '//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js' ); ./wp-content/plugins/booster-plus-for-woocommerce/includes/settings/wcj-settings-general.php: 'default' => '//ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/themes/base/jquery-ui.css', ./wp-content/plugins/booster-plus-for-woocommerce/includes/classes/class-wcj-scripts.php: $datepicker_css_path = '//ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/themes/base/jquery-ui.css'; ./wp-content/plugins/woocommerce-bookings/woocommerce-bookings.php: wp_enqueue_style( 'jquery-ui-style', '//ajax.googleapis.com/ajax/libs/jqueryui/' . $jquery_version . '/themes/smoothness/jquery-ui.min.css' ); ./wp-includes/script-loader.php: $scripts->add( 'prototype', 'https://ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js', array(), '1.7.1'); ./wp-includes/script-loader.php: $scripts->add( 'scriptaculous-root', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/scriptaculous.js', array('prototype'), '1.9.0'); ./wp-includes/script-loader.php: $scripts->add( 'scriptaculous-builder', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/builder.js', array('scriptaculous-root'), '1.9.0'); ./wp-includes/script-loader.php: $scripts->add( 'scriptaculous-dragdrop', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/dragdrop.js', array('scriptaculous-builder', 'scriptaculous-effects'), '1.9.0'); ./wp-includes/script-loader.php: $scripts->add( 'scriptaculous-effects', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/effects.js', array('scriptaculous-root'), '1.9.0'); ./wp-includes/script-loader.php: $scripts->add( 'scriptaculous-slider', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/slider.js', array('scriptaculous-effects'), '1.9.0'); ./wp-includes/script-loader.php: $scripts->add( 'scriptaculous-sound', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/sound.js', array( 'scriptaculous-root' ), '1.9.0' ); ./wp-includes/script-loader.php: $scripts->add( 'scriptaculous-controls', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/controls.js', array('scriptaculous-root'), '1.9.0'); Grep job over.
Here is the code I added to the functions.php file of my child theme:
function modify_jquery() {
if (!is_admin()) {
wp_deregister_script('jquery');
wp_register_script('jquery', 'https://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js', false, '1.9.0');
wp_enqueue_script('jquery');
}
}
add_action('init', 'modify_jquery');
wp_enqueue_scripts should do the job for the non-admin front end as you require.
add_action('wp_enqueue_scripts', 'modify_jquery', 99);
function modify_jquery(){
wp_dequeue_script( 'jquery');
wp_deregister_script( 'jquery');
wp_register_script('jquery', 'https://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js', false, '1.9.0');
wp_enqueue_script('jquery');
// repeat for your other googleapi scripts
}
Above is modified from your code - but in my case script dereg/reg and "do late" priority of 99 was unnecessary (see below).
Your grep output includes scripts which may have dependencies e.g. 'jquery' AND stylesheets. I'm not wading through through your grep; but the following is a cut and paste job example of dequeing stylesheets and handling script dependencies in child functions.php for Hemingway theme.
function dequeue_unnecessary_fonts() {
wp_dequeue_style( 'hemingway_googleFonts-css' );
wp_deregister_style( 'hemingway_googleFonts-css' );
wp_dequeue_style( 'hemingway_googleFonts' );
wp_deregister_style( 'hemingway_googleFonts' );
}
// add_action( 'wp_print_styles', 'dequeue_unnecessary_fonts',20 );
add_action( 'wp_enqueue_scripts', 'dequeue_unnecessary_fonts',20 );
// wp_print_styles (still) works for me (it can affect admin styles)
// but post WP3.3 the Codex recommends using above style functions with wp_enqueue_scripts instead
function hem_script_fix() {
wp_dequeue_script( 'hemingway_global' );
wp_enqueue_script( 'hemingway_global', get_stylesheet_directory_uri() . '/global.js', array( 'jquery' ) );
// identifies script is dependent on jquery which must be "loaded" earlier
}
add_action( 'wp_enqueue_scripts', 'hem_script_fix' );
Tried many different techniques to get jquery working in my custom wordpress theme but still haven't got anywhere.
I've included two different methods that I've tried below near the end of the code, with one commented out here.
Can anyone see why this code might not be working?
PHP:
<?php
function wpb_custom_new_menu() {
register_nav_menu('my-custom-menu',__( 'My Custom Menu' ));
}
add_action( 'init', 'wpb_custom_new_menu' );
function enqueue_stylesheets() {
wp_enqueue_style('style', get_stylesheet_directory_uri() . '/css/style.css');
wp_enqueue_style('fonts', 'https://fonts.googleapis.com/css?family=Montserrat:400,600,700|Raleway:400,500,600,700');
wp_enqueue_style( 'fontAwesome', 'https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css' );
}
/*function wp_enqeue_scripts() {
wp_register_script('navbarScroll', home_url() . '/js/navbarScroll.js', array( 'jquery' ));
wp_enqueue_script('navbarScroll');
//wp_enqueue_script( 'bootstrap-js', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js', array('jquery'), '3.3.4', true );
}*/
function navbar_script() {
wp_register_script( 'jquery.navbarScroll', get_template_directory_uri() . '/js/jquery.navbarScroll.js', array( 'jquery' ) );
wp_enqueue_script( 'jquery.navbarScroll' );
}
add_action('wp_enqueue_scripts', 'enqueue_stylesheets', 'enqueue_scripts', 'navbar_script');
My Test JS (I've used both $ and jquery):
jquery(document).ready(function() {
jquery('#jQueryTest').html('jQuery is Working');
jQuery('nav').hover(function() {
$(this).css('display', 'none');
})
/*var a = $('nav').offset().top;
$(document).scroll(function() {
if ($(this).scrollTop() > a)
{
$(this).removeClass('nav');
$(this).addClass('nav-scrolled');
} else {
$(this).removeClass('nav-scrolled');
}
});*/
});
Well it seems that you have too many parameters in your add_action function.
The add_action should have only 4 arguments as follows:
add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )
See WordPress documentation for more information.
That's what should be working just fine:
function enqueueScript() {
wp_enqueue_style('style', get_stylesheet_directory_uri() . '/css/style.css');
wp_enqueue_style('fonts', 'https://fonts.googleapis.com/css?family=Montserrat:400,600,700|Raleway:400,500,600,700');
wp_enqueue_style( 'fontAwesome', 'https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css' );
wp_enqueue_script( 'bootstrap-js', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js', array('jquery'), '3.3.4', true );
wp_register_script('navbarScroll', get_stylesheet_directory_uri() . '/js/navbarScroll.js', array( 'jquery' ), '', true );
wp_enqueue_script('navbarScroll');
wp_register_script( 'jquery.navbarScroll', get_template_directory_uri() . '/js/jquery.navbarScroll.js', array( 'jquery' ), '', true );
wp_enqueue_script( 'jquery.navbarScroll' );
}
add_action( 'wp_enqueue_scripts', 'enqueueScript' );
Note that it is not necessary to enqueue style and script separately. One method can do both by using wp_enqueue_scripts.
If you really want to separate your style and script in different function, you should call add_action this way:
add_action('wp_enqueue_scripts','wp_enqueue_scripts');
add_action( 'wp_enqueue_scripts','enqueue_stylesheets');
add_action( 'wp_enqueue_scripts','enqueue_scripts');
add_action( 'wp_enqueue_scripts','navbar_script');
If you want to load jQuery, add it to your function:
wp_enqueue_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js', array(), null, true);
Its easy if you can have a code like this to solve correctly and without confusiong. You have forgotten to pass add_action for your scripts.
function wpb_custom_new_menu() {
register_nav_menu('my-custom-menu',__( 'My Custom Menu' ));
}
add_action( 'init', 'wpb_custom_new_menu' );
function enqueue_stylesheets() {
//For registering Styles
wp_enqueue_style('style', get_stylesheet_directory_uri() . '/css/style.css');
wp_enqueue_style('fonts', 'https://fonts.googleapis.com/css?family=Montserrat:400,600,700|Raleway:400,500,600,700');
wp_enqueue_style( 'fontAwesome', 'https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css' );
//For registering Scripts files
wp_enqueue_script( 'jquery');
wp_enqueue_script( 'bootstrap-js', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js', array('jquery'), '3.3.4', true );
wp_register_script('navbarScroll', get_stylesheet_directory_uri() . '/js/navbarScroll.js', array( 'jquery' ), '', true );
wp_enqueue_script('navbarScroll');
wp_register_script( 'jquery.navbarScroll', get_template_directory_uri() . '/js/jquery.navbarScroll.js', array( 'jquery' ), '', true );
wp_enqueue_script( 'jquery.navbarScroll' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_stylesheets' );
Hope this works for you.
THanks
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