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
Related
I'm using WordPress version 5.7.2 and when I upgrade it to php version 7.4.19 I get these errors:
Failed opening 'default' for inclusion (include_path='.:/usr/lib/php7.4') wp-includes/template-loader.php on line 106
Warning: include(default): failed to open stream: No such file or directory in /homepages/1/d229455270/htdocs/clickandbuilds/WordPress/DaseCMS/wp-includes/template-loader.php on line 106
This happens when I activate the plugin reactpress. This is the piece of code where the error occurs:
/**
* Filters the path of the current template before including it.
*
* #since 3.0.0
*
* #param string $template The path of the template to include.
*/
$template = apply_filters( 'template_include', $template );
if ( $template ) {
include $template; //Error in this line
} elseif ( current_user_can( 'switch_themes' ) ) {
$theme = wp_get_theme();
if ( $theme->errors() ) {
wp_die( $theme->errors() );
}
Why is this happening? How can I fix it? I see that is compatible with my WordPress version...
And with my php version...
Thank you in advance
Why is this happening?
Because there's a mistake in the Reactpress_Public::repr_change_page_template() method (see line 99 in wp-content/plugins/reactpress/public/class-reactpress-public.php) which is hooked onto template_include.
The author should check if the value of the _wp_page_template metadata (which stores the path of a custom page template) is not default (which is the default value) and only if so, then should the $template value be set to the metadata value.
And if one doesn't do that check, then we'd end up with include 'default' which then emits the error/warning in question ("Failed opening 'default' for inclusion").
How can I fix it?
Please contact the plugin support and ask them to fix the issue ASAP, but for the time being, you may just change the conditional here to: (* change the entire "if")
if (!empty($meta['_wp_page_template'][0]) && $meta['_wp_page_template'][0] != $template && // wrapped
'default' !== $meta['_wp_page_template'][0] // check if the value is NOT "default"
) {
$template = $meta['_wp_page_template'][0];
}
Yes, you shouldn't modify core plugin files; but this is a special case, because the plugin needs a fix, which hopefully will come in the plugin's next release.
Alternate solution without modifying the plugin files
.. is by overriding the template using the same hook:
// Add to the theme functions.php file:
add_filter( 'template_include', 'my_fix_template_include', 100 );
function my_fix_template_include( $template ) {
if ( 'default' === $template && is_page() ) { // * the plugin uses is_page()
$template = get_page_template();
}
return $template;
}
I've put some custom code in my active child theme's functions.php. I'm trying to enqueue some style on a admin page. However, a style enqueued in admin_enqueue_scripts hook gets automatically removed and after debugging I found that its not present in the very next hook i.e. admin_print_styles.
Here's some code in active child theme's functions.php which I used for debugging purposes:
function debug_enqueue_admin_scripts() {
wp_enqueue_style( 'gforms_datepicker_css', GFCommon::get_base_url() . "/css/datepicker{$min}.css", null, GFCommon::$version );
if( wp_style_is( 'gforms_datepicker_css' ) {
// NOTE: This runs and I am able to view the following log
error_log( __FUNCTION__ . ': datepicker_css is enqueued.' );
}
}
add_action( 'admin_enqueue_scripts', 'debug_enqueue_admin_scripts', 11 );
function check_if_still_enqueued() {
if( wp_style_is( 'gforms_datepicker_css', 'registered' ) ) {
error_log( __FUNCTION__ . ' datepicker_css registered.');
} else {
// NOTE: It gets in this else block and following output is logged
error_log( __FUNCTION__ . ' datepicker_css **NOT** registered.');
}
}
add_action( 'admin_print_styles', 'check_if_still_enqueued' );
I'm not deregistering the gforms_datepicker_css anywhere, but its getting removed maybe due to some plugin.
While debugging, I've gone further and inspected if it was deregistered by putting extra line in WordPress core class method WP_Dependencies::remove() located here as following.
public function remove( $handles ) {
// NOTE: Check if deregistered
error_log( __METHOD__ . ' ' . var_export( $handles, true ) );
foreach ( (array) $handles as $handle )
unset($this->registered[$handle]);
}
But I'm unable to see log output with gforms_datepicker_css in it from this method.
I'm unsure why the style is getting removed from enqueue list. Can anybody please help me debugging this behaviour and find the solution?
The scripts and styles were not loading on admin pages because the No-Conflict Mode setting of Gravity Forms plugin was turned ON. As the setting is designed to do so:
As described in Gravity Forms documentation:
To temporarily resolve the issue, go to Forms > Settings and enable No
Conflict mode. This should stop third party scripts from writing to
Gravity Forms administration pages and allow you to do the things you
need.
The issue resolved after turning OFF the No-Conflict Mode.
I have my wordpress setup on IIS and it is rewritten. Because of that I found some probably bug in wordpress in wp_includes/canonical.php file.
I think that when I update my wordpress that all my changes will disapear. I can't wait for wordpress fix this (I don't even know if it is bug or how to report one) because that "bug" causes my homepage to go in redirect loop.
I made some change in redirect_canonical function in that file specificly this change:
function redirect_canonical( $requested_url = null, $do_redirect = true ) {
///some other code
if ( ! $requested_url && isset( $_SERVER['HTTP_HOST'] ) ) {
// build the URL in the address bar
$requested_url = is_ssl() ? 'https://' : 'http://';
//$requested_url=$_SERVER['HTTP_HOST'] //I deleted this because my URL is rewritten
$asParts = parse_url( $sURL ); // PHP function
$requested_url .= $asParts['host'] ;
$requested_url .= $_SERVER['REQUEST_URI'];
How to make this change resistant to wordpress updates?
WordPress have the hooks concept to change the core functions and process. All hooks are defined via apply_filters and do_action. The hooks are different in filter apply_filters and action - do_action.
You should read about: https://codex.wordpress.org/Plugin_API/Hooks
You should never change the core, because maintenance, function and more. In your example function redirect_canonical have also hooks, like redirect_canonical (Doku) (in source). However it give much more and it is in depends on your goal, requirements.
As small example to change the return value of the function via the filter hook see the code example below.
add_filter( 'redirect_canonical', function ( $redirect_url, $requested_url ) {
// I don't know what is the var $sURL in your code.
$requested_url = parse_url( $sURL );
// Leave here the logic to change the result for $redirect_url.
return $redirect_url;
}, 10, 2 );
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/
When I try to access the plugins or themes section of my wordpress site from the admin panel I am presented with a blank screen. When I run the logs I get the following error:
Navigating to wp-admin/plugins.php:
PHP Fatal error: Call to undefined function wp_json_encode() in /var/lib/openshift/{userID}/app-root/data/current/wp-includes/update.php on line 277
Navigating to wp-admin/themes.php:
PHP Fatal error: Call to undefined function wp_json_encode() in /var/lib/openshift/{userID}/app-root/data/current/wp-includes/update.php on line 440
Solutions online indicated that I should re-add the function, or re-install Wordpress. Without access to the core files, I downloaded a local repository of the application (but noticed it did not contain any of the plugins or themes I had uploaded via the admin interface).
I extracted a plugin and theme (placing them in the respective directories) then pushed the changes to production in the hopes that it would extract and re-install an updated version of wordpress. I then restarted the app.
The error still persists and I can not validate if the plugin or theme I uploaded were installed. Is there a way to refresh or reinstall a wordpress instance on Openshift?
I'm wondering how I can fix this issue without creating a new gear and migrating my data via the database. Note: Front end is working fine.
Version of Wordpress: 4.1.1
I ended up connecting to the app via SFTP and modified the file the following directly
/var/lib/openshift/{userID}/app-root/data/current/wp-includes/functions.php
and added the following function:
function wp_json_encode( $data, $options = 0, $depth = 512 ) {
/*
* json_encode() has had extra params added over the years.
* $options was added in 5.3, and $depth in 5.5.
* We need to make sure we call it with the correct arguments.
*/
if ( version_compare( PHP_VERSION, '5.5', '>=' ) ) {
$args = array( $data, $options, $depth );
} elseif ( version_compare( PHP_VERSION, '5.3', '>=' ) ) {
$args = array( $data, $options );
} else {
$args = array( $data );
}
$json = call_user_func_array( 'json_encode', $args );
// If json_encode() was successful, no need to do more sanity checking.
// ... unless we're in an old version of PHP, and json_encode() returned
// a string containing 'null'. Then we need to do more sanity checking.
if ( false !== $json && ( version_compare( PHP_VERSION, '5.5', '>=' ) || false === strpos( $json, 'null' ) ) ) {
return $json;
}
try {
$args[0] = _wp_json_sanity_check( $data, $depth );
} catch ( Exception $e ) {
return false;
}
return call_user_func_array( 'json_encode', $args );
}
Ref: https://wordpress.org/support/topic/fatal-error-call-to-undefined-function-wp_json_encode-in