how to override parent functions.php in child functions.php - php

I am looking for some suggestions on how to override or replace some functions that are loading in a parent theme and move them into the child theme so we can make alterations to the files themselves in the child theme.
We have duplicated the structure and files of the parent theme into the child theme, but can't yet find a way to prevent the parent theme from loading and load the child theme files instead.
Essentially it's all the require files listed below that we will duplicate and alter in the child theme, but need to find some way to override the parents functions.php.
We have tried multiple ways to do this but just can't seem to get it to work so far.
This is the current parent functions.php file:
/**
* moto functions and definitions
*
* #package moto
*/
if ( ! function_exists( 'moto_setup' ) ) :
/**
* Sets up theme defaults and registers support for various WordPress features.
*
* Note that this function is hooked into the after_setup_theme hook, which
* runs before the init hook. The init hook is too late for some features, such
* as indicating support for post thumbnails.
*/
function moto_setup() {
global $pagenow;
if ( is_admin() && 'themes.php' == $pagenow && isset( $_GET['activated'] ) ) {
wp_redirect(admin_url("options-general.php?page=moto-system-status")); // Your admin page URL
exit();
}
/* snip irrelevant code */
}
endif; // moto_setup
add_action( 'after_setup_theme', 'moto_setup' );
/* snip irrelevant code */
add_action( 'after_setup_theme', 'moto_content_width', 0 );
/**
* Register widget area.
*
* #link http://codex.wordpress.org/Function_Reference/register_sidebar
*/
function moto_widgets_init() {
/* snip irrelevant code */
}
add_action( 'widgets_init', 'moto_widgets_init' );
/**
* Implement the Custom Header feature.
*/
require get_template_directory() . '/function/custom-header.php';
/**
* Custom template tags for this theme.
*/
require get_template_directory() . '/function/template-tags.php';
/**
* Custom functions that act independently of the theme templates.
*/
require get_template_directory() . '/function/extras.php';
/**
* Customizer additions.
*/
require get_template_directory() . '/function/customizer.php';
/**
* Load Jetpack compatibility file.
*/
require get_template_directory() . '/function/jetpack.php';
require_once get_template_directory() . '/include/aq_resizer.php';
require_once get_template_directory() . '/include/moto-sys-req.php';
require_once get_template_directory() . '/include/moto-enqueue.php';
require_once get_template_directory() . '/include/moto-functions.php';
require_once get_template_directory() . '/include/theme_plugin/plugin-activate-config.php';
require_once get_template_directory() . '/include/wordpress-reset.php';
Any suggestions?
Thank you in advance.

Related

Wordpress plugin register_activation_hook doesnt work

So In my main plugin file I have this
class RegistrationHooks
{
/**
* #var string
*/
protected string $plugin_dir;
public function __construct()
{
$this->plugin_dir = WP_PLUGIN_DIR . '/invoices';
}
function active_invoices()
{
require_once $this->plugin_dir . '/includes/class-invoices.php';
new Invoices();
}
}
$hooksInstance = new RegistrationHooks();
register_activation_hook(__FILE__, array($hooksInstance, 'active_invoices'));
And inside My Invoices class I have this:
class Invoices
{
public function __construct()
{
$this->load_dependencies();
$this->define_admin_hooks();
}
private function load_dependencies()
{
$path = WP_PLUGIN_DIR . '/invoices';
if ($path) {
require_once $path . '/admin/class-admin.php';
}
}
private function define_admin_hooks()
{
$plugin_admin = new Admin();
add_action('admin_menu', array($plugin_admin, 'add_plugin_admin_menu'));
}
}
And Inside my Admin class I have simple method to add item to menu
class Admin {
public function add_plugin_admin_menu()
{
add_menu_page(
'Page Title',
'Menu Title',
'edit_posts',
'menu_slug',
);
}
}
But when I activate Plugin item doesnt get added to admin menu. When I Place my Invoices class inside my main Plugin class it works.
You're misusing register_activation_hook(). That hook only runs when you Activate your plugin (immediately after installing it, usually). It's used for things like creating plugin-specific tables and prepopulating options. Immediately after activation, WordPress does a redirect and starts a new page view, so anything non-persistent you do (like adding other actions) in the activation hook vanishes immediately.
Your code only requires your class-invoices.php file from within your registration hook. So it doesn't run in ordinary page views or dashboard views.
You can do this, in your toplevel plugin file, to require your admin code, and construct it, on dashboard views.
function invoices_admin_init () {
require_once( plugin_dir_path( __FILE__ ) . '/includes/class-invoices.php' );
new Invoices();
}
add_action( 'admin_init', invoices_admin_init );
It's good practice to require code only when it's needed (in this example, only on dashboard pageviews) to reduce the overhead your plugin causes.
Be aware that the action function you declare pollutes WordPress's global name space, so give it a distinct name no other plugin writer is likely to use.

where do i put woocommerce image gallery enable code

Woocommerce took out the option for enabling a lightbox feature for your image gallery earlier this year. They have in their documentation to add code if you want to enable the gallery features but don’t actually say where.
https://woocommerce.wordpress.com/2017/02/28/adding-support-for-woocommerce-2-7s-new-gallery-feature-to-your-theme/
This is a significant frontend change that can be broken down in to three separate new features;
• Image zoom / magnification
• Lightbox
• Slider
To enable each of these features in your theme you must declare support using add_theme_support() like so:
add_action( 'after_setup_theme', 'yourtheme_setup' );
function yourtheme_setup() {
add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' );
}
This allows you the flexibility to pick and choose exactly which features you want to include/exclude in your theme or at your store.
I am not a developer, I don’t have a developer (and shame on WC for not making this an option that end users can opt for or not without having to add code!)
I need to know where to put this code. I am using a child theme called mystile1. I have files called “Theme Functions (function.php)” and one called “custom.css” that’s says it is specifically for adding code to modify my child theme styles.
I don’t know which file I should put the above coding in and where. Nowhere does each of these files have a line called “after_setup_theme” So would I be safe in just adding the code as follows in one of those files (which one?) replacing “yourtheme” with the name of my theme:
add_action( 'after_setup_theme', 'mystile1_setup' );
function mystile1_setup() {
add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' );
}
Or any other suggestions are greatly appreciated.
Thank you.
IN RESPONSE:
Below is what is in my functions.php file. would I put the code at the top in between new brackets or down in the section that says:
/-----------------------------------------------------------------------------------/
/* You can add custom functions below /
/-----------------------------------------------------------------------------------*/
function mystile1_setup() {
add_theme_support( 'wc-product-gallery-zoom' );
add_theme_support( 'wc-product-gallery-lightbox' );
add_theme_support( 'wc-product-gallery-slider' );
}
?>
"MY FUNCTIONS.PHP FILE" INCLUDES
<?php
// File Security Check
if ( ! empty( $_SERVER['SCRIPT_FILENAME'] ) && basename( __FILE__ ) == basename( $_SERVER['SCRIPT_FILENAME'] ) ) {
die ( 'You do not have sufficient permissions to access this page!' );
}
?>
<?php
/-----------------------------------------------------------------------------------/
/* Start WooThemes Functions - Please refrain from editing this section /
/-----------------------------------------------------------------------------------*/
// Define the theme-specific key to be sent to PressTrends.
define( 'WOO_PRESSTRENDS_THEMEKEY', 'zdmv5lp26tfbp7jcwiw51ix9sj389e712' );
// WooFramework init
require_once ( get_template_directory() . '/functions/admin-init.php' );
/-----------------------------------------------------------------------------------/
/* Load the theme-specific files, with support for overriding via a child theme.
/-----------------------------------------------------------------------------------/
$includes = array(
'includes/theme-options.php', // Options panel settings and custom settings
'includes/theme-functions.php', // Custom theme functions
'includes/theme-actions.php', // Theme actions & user defined hooks
'includes/theme-comments.php', // Custom comments/pingback loop
'includes/theme-js.php', // Load JavaScript via wp_enqueue_script
'includes/sidebar-init.php', // Initialize widgetized areas
'includes/theme-widgets.php', // Theme widgets
'includes/theme-install.php', // Theme installation
'includes/theme-woocommerce.php' // WooCommerce options
);
// Allow child themes/plugins to add widgets to be loaded.
$includes = apply_filters( 'woo_includes', $includes );
foreach ( $includes as $i ) {
locate_template( $i, true );
}
/-----------------------------------------------------------------------------------/
/* You can add custom functions below /
/-----------------------------------------------------------------------------------*/
// CUSTOM FUNCTION ADDED TO ADDRESS LACK OF ADD-TO-CART BUTTONS ON VARIABLE ITEMS
// AS DOCUMENTED AT: http://wordpress.org/support/topic/plugin-woocommerce-excelling-ecommerce-checkout-button-not-showing-on-woo-commerce-product/page/2?replies=36#post-3263097
function mv_my_theme_scripts()
{
wp_enqueue_script('add-to-cart-variation', get_template_directory_uri() . '/js/add-to-cart-variation.js',array('jquery'),'1.0',true);
}
add_action('wp_enqueue_scripts','mv_my_theme_scripts');
/-----------------------------------------------------------------------------------/
/* Don't add any code below here or the sky will fall down /
/-----------------------------------------------------------------------------------*/
?>`
You have to put the code in your function.php between <?php and ?> tags.
As additional note: you can put all of these gallery' effects or only few of them on your site. For example if performance of your site is degrading you can delete or put // to
add_theme_support( 'wc-product-gallery-zoom' );
or to other effects.

Cron event is not executed in custom Wordpress plugin

I have a site where i need to import data daily from an external url so i made a plugin to handle this. So far so good, but the thing is that my cron event doesn't work. I installed Crontrol plugin to test the event, but nothing happens. I see my hook name in the list, but when i click on 'Run now' i get a message that the cron event is successfully executed, but the data isn't imported.
I've searched through a lot of recourses online (for example), but somehow all the solutions posted elsewhere don't seem to work for me. I must be missing a step somewhere.
The plugin is called import-data and in wp-content/plugins/import-data/ i have import-data.php:
<?php
/**
* Plugin Name: Import data
* Plugin URI:
* Description: Import data
* Version: 1.0.0
* Author:
* Author URI:
* License: GPL2
*/
// Block direct acces to file
defined('ABSPATH') or die();
// Include functions
require_once dirname( __FILE__ ).DIRECTORY_SEPARATOR.'functions.php';
// Include class
require_once dirname( __FILE__ ).DIRECTORY_SEPARATOR.'lib/class.import_data.php';
/**
* #desc iterate through all posts and update information
*/
function import_data(){
$wp_query = new WP_Query(
array(
'post_type' => 'post',
'post_status' => 'publish',
)
);
if($wp_query->have_posts()){
while($wp_query->have_posts()){
$wp_query->the_post();
$post_id = $wp_query->post->ID;
$external_id = get_field(trim(get_option('acfname_external_id')));
// Execute plugin
Import_Data::getInstance()->fetchDetails($external_id, $post_id);
}
wp_reset_postdata();
}
}
/**
* Set cron
*/
function my_event(){
if(!wp_next_scheduled('import_data')){
wp_schedule_event(time(), 'daily', 'import_data');
}
}
add_action('wp', 'my_event');
function unset_event(){
wp_clear_scheduled_hook('import_data');
}
register_deactivation_hook(__FILE__, 'unset_event');
I know that the method fetchDetails() works because i tested the output before and when i manually run it (i've added a shortcode to import_data() and used that on a demo page) the data gets imported, but the cron settings above don't.
In functions.php are only admin page settings.
This are my first steps in the world of plugin development for Wordpress so i can image that i miss an important hook or filter (or whatever), but i just can't find what it is. Perhaps some initialisation?
First of you should prefix your global php functions to avoid conflicts with other plugins, themes or core.
I would use the activation hook to schedule the event, here is how I would do this:
<?php
/**
* Plugin Name: Import data
* Plugin URI:
* Description: Import data
* Version: 1.0.0
* Author:
* Author URI:
* License: GPL2
*/
// Block direct acces to file
defined('ABSPATH') or die();
// Include functions
require_once dirname( __FILE__ ).DIRECTORY_SEPARATOR.'functions.php';
// Include class
require_once dirname( __FILE__ ).DIRECTORY_SEPARATOR.'lib/class.import_data.php';
// Register activation / deactivation hooks
register_deactivation_hook( __FILE__, 'ip_deactivation_func' );
register_activation_hook( __FILE__, 'ip_activation_func' );
// The plugin activation function
function ip_activation_func(){
// Do not forget to namespace your hooks also
if( !wp_next_scheduled( 'ip_import_data' ) ){
wp_schedule_event( time(), 'daily', 'ip_import_data' );
}
}
// The plugin deactivation function
function ip_deactivation_func(){
wp_clear_scheduled_hook( 'ip_import_data' );
}
// Add the action event hook
add_action( 'ip_import_data', 'ip_do_import_data' );
// Your actual event code:
function ip_do_import_data(){
$wp_query = new WP_Query(
array(
'post_type' => 'post',
'post_status' => 'publish'
)
);
if( $wp_query->have_posts() ){
while($wp_query->have_posts()){
$wp_query->the_post();
// Added this part, no need to use: $wp_query object here!
global $post;
$post_id = $post->ID;
$external_id = get_field( trim( get_option( 'acfname_external_id' ) ) );
// Execute plugin
Import_Data::getInstance()->fetchDetails( $external_id, $post_id );
}
wp_reset_postdata();
}
}
I do not know about your event code, you might need to run it to make sure its working correctly.
Learn more about WP cron here:
https://developer.wordpress.org/plugins/cron/
Learn more about activation / deactivation hooks here:
https://developer.wordpress.org/plugins/the-basics/activation-deactivation-hooks/
And a good plugin to debug wp cron events:
https://wordpress.org/plugins/wp-crontrol/

why my plugin don't traslate wordpress

i'm doing a wordpress plugin.
my_plugin.php
/*
* Plugin Name: my plugin
* Author: Vendetta
* Author URI: http://abel-olguin.com
* Text Domain: my_plugin
* Domain Path: /languages
*/
*.po and *.mo files are in lenguages and i call traslate whit
__("text in po", "my_plugin")
files *.po and *.mo in directory languages names:
my_plugin-en_US.po, my_plugin-es_ES.po...
but strings are not translated, always show everything in English and my browser is in spanish.
what it's wrong?
You need to load your plugin’s translations files using the function load_plugin_textdomain().
function my_plugin_load_plugin_textdomain() {
load_plugin_textdomain( 'my-plugin', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
}
add_action( 'plugins_loaded', 'my_plugin_load_plugin_textdomain' );
Read more here: http://ottopress.com/tag/internationalization/

Getting an error on wordpress theme

I'm making a website using wordpress and a custom theme. I tested the website on my computer using XAMP and it worked perfectly. But when I uploaded my code to the web hosting, it gave this error:
ERROR : Parse error: syntax error, unexpected '{' in /home/a5709387/public_html/wp-content/themes/gameaddict/themeOptions/functions.php on line 1
Can anyone help? Here's my code:
<?php
if ( !function_exists( 'optionsframework_init' ) )
{
/*-----------------------------------------------------------------------------------*/
/* Options Framework Theme
/*-----------------------------------------------------------------------------------*/
/* Set the file path based on whether the Options Framework Theme is a parent theme or child theme */
define('OPTIONS_FRAMEWORK_URL', get_template_directory() . '/themeOptions/admin/');
define('OPTIONS_FRAMEWORK_DIRECTORY', get_template_directory_uri() . '/themeOptions/admin/');
require_once (OPTIONS_FRAMEWORK_URL . 'options-framework.php');
}
/*
* This is an example of how to add custom scripts to the options panel.
* This one shows/hides the an option when a checkbox is clicked.
*/
add_action('optionsframework_custom_scripts', 'optionsframework_custom_scripts');
function optionsframework_custom_scripts() { ?>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#example_showhidden').click(function() {
jQuery('#section-example_text_hidden').fadeToggle(400);
});
if (jQuery('#example_showhidden:checked').val() !== undefined) {
jQuery('#section-example_text_hidden').show();
}
});
</script>
<?php
}
/*
* Turns off the default options panel from Twenty Eleven
*/
add_action('after_setup_theme','remove_twentyeleven_options', 100);
function remove_twentyeleven_options() {
remove_action( 'admin_menu', 'twentyeleven_theme_options_add_page' );
}
?>

Categories