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/
Related
So I'm trying to extend UltimateMember's functionality with a custom plugin called "testFamily". In the plugin I have testFamily.php, with these functions:
require plugin_dir_path( __FILE__ ) . '../ultimate-member/includes/core/um-actions-login.php';
function register_hooks() {
add_action( 'um_user_login', 'onLogin', 999, 1 );
add_action( 'init' , 'testInit');
}
function testInit() {
echo "Hello";
}
function onLogin($userid, $args) {
// do some stuff after logging in...
}
register_hooks();
The init callback works fine, but the um_user_login callback doesn't run at all. I even set the function code to die(); and it doesn't work.
Calling function_exists('um_user_login') returns true.
I've also tried to implement it as a child theme like the docs say, but it doesn't run then either.
I've tried example code from the website
I used a different hook: um_on_login_before_redirect and it worked just fine.
I have the following use case. I have created a custom "search" functionality, all it is an input box that is using some API fetch and jquery magic.
Essentially, I want to add this search function into its own custom sidebar navigation widget via a plugin installation.
I wish to create a plugin that registers a custom "menu" sidebar widget. The sidebar widget will drop this search functionality to were ever the widget is "dragged and dropped". The display is for only the front of the site. You know, how the default search widget works.
Some Facts:
I have created a boiler plated plugin in wp-contents, it had a blank
activate, deactivate and uninstall function within the mail plugin class and I have hooked the activate and deactivate button.
The source code for the "custom search functionality" is within the
same folder as the plugin.
Folder scheme looks like:
MBE PLUGIN
index.php
mbe-plugin.php
api-search.html
assets
css
scripts
ETC
In my plugin.php file, what must I include in the "activation" function to achieve this?
This is my boilerplate plugin code... pretty empty
.
<?php
/**
* #package MBEPlugin
*/
/*
Plugin Name: MBE Plugin
Plugin URI: http://localhost/mbeTest/plugin
Description: This plugin is for the search modal for MBE website. Project 'accordion'
Version: 1.0.0
Author: Erick Guerra
Author URI: http://bashs3c.com
License: GPLv2 or later
Text Domain: mbe-plugin
*/
// Secuirty measure, checks variable in env to ensure we are in WP
if ( ! defined( 'ABSPATH' ) ) {
echo 'These are not the drones you are looking for!';
exit;
}
class MbePlugin {
function activate() {
$searchHTML = file_get_contents(plugins_url('api-search.html', __FILE__));
echo $searchHTML;
// generate a CPT
// Flush the rewrite rules
}
function deactivate() {
// Flush rewrite rules
}
function uninstall() {
// delete CPT
// Delete all the plugin data from DB
}
function displaySearch() {
$searchHTML = file_get_contents(plugins_url('api-search.html', __FILE__));
echo $searchHTML;
}
}
if (class_exists('MbePlugin')) {
$mbePlugin = new MbePlugin();
}
//activation
register_activation_hook( __FILE__, array( $mbePlugin, 'activate' ) );
//deactivation
register_deactivation_hook( __FILE__, array( $mbePlugin, 'deactivate' ) );
//uninstall
//
//add_actions('wp_enqueue_scripts', 'search_init');
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.
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.
I am working on a theme (http://lillykauffman.com/wordpress/2017/06/26/hello-world/), which I've done before, but if you try to post a comment, you will be redirected to a blank page. This also happens on the other WP themes such as twentysixteen. Here is the code I have on wp-comments-post.php:
<?php
/**
* Handles Comment Post to WordPress and prevents duplicate comment posting.
*
* #package WordPress
*/
if ( 'POST' != $_SERVER['REQUEST_METHOD'] ) {
$protocol = $_SERVER['SERVER_PROTOCOL'];
if ( ! in_array( $protocol, array( 'HTTP/1.1', 'HTTP/2', 'HTTP/2.0' ) ) ) {
$protocol = 'HTTP/1.0';
}
header('Allow: POST');
header("$protocol 405 Method Not Allowed");
header('Content-Type: text/plain');
exit;
}
/** Sets up the WordPress Environment. */
require( dirname(__FILE__) . '/wp-load.php' );
nocache_headers();
$comment = wp_handle_comment_submission( wp_unslash( $_POST ) );
if ( is_wp_error( $comment ) ) {
$data = intval( $comment->get_error_data() );
if ( ! empty( $data ) ) {
wp_die( '<p>' . $comment->get_error_message() . '</p>', __( 'Comment Submission Failure' ), array( 'response' => $data, 'back_link' => true ) );
} else {
exit;
}
}
$user = wp_get_current_user();
/**
* Perform other actions when comment cookies are set.
*
* #since 3.4.0
*
* #param WP_Comment $comment Comment object.
* #param WP_User $user User object. The user may not exist.
*/
do_action( 'set_comment_cookies', $comment, $user );
$location = empty( $_POST['redirect_to'] ) ? get_comment_link( $comment ) :
$_POST['redirect_to'] . '#comment-' . $comment->comment_ID;
/**
* Filters the location URI to send the commenter after posting.
*
* #since 2.0.5
*
* #param string $location The 'redirect_to' URI sent via $_POST.
* #param WP_Comment $comment Comment object.
*/
$location = apply_filters( 'comment_post_redirect', $location, $comment );
wp_safe_redirect( $location );
exit;
I don't know why this isn't working since it's straight from WP, and other people online had this error because their file was blank, which is not my case. And all of this code looks to be in order. My MySQL version is 5.7 so that shouldn't be the issue, but at this point, I don't even know what's wrong anymore - the code, the installation, my hosting. Any help would be greatly appreciated.
Update: My host advertises full blog, comment and discussion board functionality but I contacted them and they were like, "not really." Apparently they don't support comments or email sending. What a waste. Thanks for everyone's help!
When you look your problem page with inspect manager, you can see that your problem is a 410 code error.
410 Gone
Indicates that the resource requested is no longer available at the server and will not be available again.
This error can occur in several ways.
Here it's the list of somes checkpoint to help you to find the problem:
1. Enable debugging in wp-config.php
// Enable WP_DEBUG mode
define( 'WP_DEBUG', true );
// Enable Debug logging to the /wp-content/debug.log file
define( 'WP_DEBUG_LOG', true );
// Enable display of errors and warnings
define( 'WP_DEBUG_DISPLAY', true );
#ini_set( 'display_errors', 1 );
And test to know if your error appear. You'll be able to fix it quickly.
2. Reset permalink
Don't know why but, sometimes, Wordpress have some weird error du to a old permalink.
I suggest you to
Going to Settings -> Permalinks
Switch permalink setting,
Save,
Replace it to your current configuration,
save it again.
3. Deactivating all plugins.
if its works, activate plugin one by one to be able to detect which one create this error.
4. Refresh your .htaccess
place the basic wordpress .htaccess. Sometimes, some plugins change rules from your .htaccess and provoke many redirections error.
5. Switch your theme for a default theme without modification
It help you to know if a custom rules in your theme create this error.
6. Re-upload the wp-admin and wp-includes from fresh install
7. Reset folders write/read permissions
you can read Changing File permissions in Wordpress to help you to know what you should change and what it should do.
8. ULTIMATELY, Create a fresh install of Wordpress