I need to add 'global $woocommerce;' in the wordpress my-ac-plugin.php to pull in the users country code for my caching but I'm getting a 500 error.
Does anyone know how I can do this, or any ideas what's wrong?
Here's my code...
if (!defined('WPINC')) {
exit('Do NOT access this file directly.');
}
function my_ac_plugin()
{
$ac = $GLOBALS['comet_cache_advanced_cache'];
$ac->addFilter('comet_cache_version_salt',
'my_ac_version_salt_shaker');
}
function my_ac_version_salt_shaker($version_salt)
{
global $woocommerce;
$customer_country = $woocommerce->customer->get_country();
$version_salt .= $customer_country;
geoip_close($gi);
return $version_salt;
}
my_ac_plugin();
Follow these steps to help in troubleshooting:
Go FTP into your website
Open the file named wp-config.php
Find the line where you see define('WP_DEBUG', false);
Change false to true and save the file
The next time you refresh the page you were getting an error on you should now see more descriptive error messages which will help in troubleshooting.
Related
I'm using this Flash Messages Script for a simple redirect and flash message system.
Everything works fine on my apache localhost, but as soon as I upload it to a server (also apache) it doesn't work. It sets the sessions and also displays the messages correctly, but it doesn't unset the messages afterwards. Now I have a whole bunch of "Flash messages" on my website and they'll get more and more unless you close your browser to unset all sessions forcefully.
I've already read the documentation like a thousand times and also searched in the Flash Messages script on the server for any errors. I couldn't find any.
Maybe you guys can help me. The host where I'll deploy my website is strato.com.
Edit: I found a cookie called PHPSESSID in my browser informations. Maybe this could be helpfull.
Constructor:
public function __construct()
{
// Generate a unique ID for this user and session
$this->msgId = sha1(uniqid());
// Create session array to hold our messages if it doesn't already exist
if (!array_key_exists('flash_messages', $_SESSION)) $_SESSION['flash_messages'] = [];
}
Clear session function:
protected function clear($types=[])
{
if ((is_array($types) && empty($types)) || is_null($types) || !$types) {
unset($_SESSION['flash_messages']);
} elseif (!is_array($types)) {
$types = [$types];
}
foreach ($types as $type) {
unset($_SESSION['flash_messages'][$type]);
}
return $this;
}
Add Sessions:
public function add($message, $type=self::defaultType, $redirectUrl=null, $sticky=false)
{
// Make sure a message and valid type was passed
if (!isset($message[0])) return false;
if (strlen(trim($type)) > 1) $type = strtolower($type[0]);
if (!array_key_exists($type, $this->msgTypes)) $type = $this->defaultType;
// Add the message to the session data
if (!array_key_exists( $type, $_SESSION['flash_messages'] )) $_SESSION['flash_messages'][$type] = array();
$_SESSION['flash_messages'][$type][] = ['sticky' => $sticky, 'message' => $message];
// Handle the redirect if needed
if (!is_null($redirectUrl)) $this->redirectUrl = $redirectUrl;
$this->doRedirect();
return $this;
}
I fixed it. It was due an change in PHP 7.1 in the php.ini file. As soon as I downgraded my PHP version to PHP 7.0 everything worked fine again.
I hope this will help a lot of people. At least you've got some starting point now.
I have a form made with Contact Form 7 and I need to save the attachment in a folder but move_uploaded_file() or copy doesn't work. Do you know how to solve this problem? Thanks!!
add_action('wpcf7_before_send_mail', 'save_form' );
function save_form( $wpcf7 ) {
if (move_uploaded_file($_FILES["photo1"]["tmp_name"],$_FILES["photo1"]["name"]))
{ /* CORRECT */ }
else { /* ERROR */ } }
Hope you can help me, I don't know how continue. In a php file this function works but in Wordpress it's impossible. Thanks very much!!!!
I've written a function to change the role of a member in response to a Membermouse push notification. The code fails with the message "Fatal error: Call to undefined function wp_update_user()...". This implies the function has not been called but the folder is in the WP environment and is called by a WP Plugin function. Although its not advised, I tried various ways to require the user.php file (where wp_update_user is located) in the code and none worked. I'm at a loss since I believe the code is correctly written but I'm not even sure about that at this point. The custom script file (below) is in a custom folder in the root directory.
<?php
// Custom script to change a members role to Spectator upon cancellation
if(!isset($_GET["event_type"])) {
// event type was not found, so exit
echo "Exit";
exit;
} else {
$status = $_GET["status"];
if($status == 2) {
$eventType = $_GET["event_type"];
$user_id = $_GET["member_id"];
$newrole = "bbp_spectator";
$user_id = wp_update_user( array(
'ID' => $user_id,
'role' => $newrole
) );
if (is_wp_error($user_id)) {
// There was an error, probably that user doesn't exist.
echo "Error";
} else {
// Success!
echo "Role Changed to Spectator";
}
}
}
?>
https://codex.wordpress.org/Plugin_API/Action_Reference. If you just include without an action hook that runs after the user is set (init is a good one), then there is no user and the function doesn't exist yet.
function prefix_my_function_name() {
//your code
}
add_action( 'init' , 'prefix_my_function_name' );
I fixed it... I was using the wrong path for the require statement. I love the help on the web, but the multitude of responses on various forums shows so many ways to do things. It never occurred to me to keep it simple. I added the following to the top of the code:
require_once("wp-includes/user.php");
All the comments from previous posts with similar problems were proposing various ways of saying the same thing but this one worked.
I'm writing a WordPress plugin. I want to display a custom message after a post is saved. This message will depend on the outcome of function called when the post is saved.
Here's my code:
add_action('save_post', 'my_save_post_function');
function my_save_post_function() {
global $msg;
$msg = "Foo bar";
...
}
add_filter('post_updated_messages', 'my_post_updated_messages_function');
function my_post_updated_messages_function($messages) {
global $msg;
$messages["post"][1] = $msg; // !! $msg is undefined !!
...
}
Why is $msg undefined?
Is there any way I can get a result out of a save_post action? I've tried all sorts of tricks. Even the $_POST data seems to have been blown away by the time admin messages are shown.
have you tried session ? i think your problem will be fixed .
take a look at :
http://www.php.net/manual/en/function.session-start.php
Ok, am using traditional php, no frameworks, nothing, I am using simple procedural way, now my question is I was searching for a while but am not getting an answer to my question, I am not using .htaccess files as of now, but I really need to understand how 404 error works? I am having a website, where I show post's related to category, say category=php, so I pass this as a get request
$_GET['category'] == 'php';
Now currently what am doing is something like this :
$pocategory = $_GET['category'];
if($pocategory == 'php' || $pocategory == 'javascript') {
//Then show related posts
} else {
header('Location:404.php');
exit;
}
I mean I just want php and javascript as valid request's value, rest I want to redirect to 404 but am not understanding how to do it so I did this way, what if am having more than 50 categories? I cant list them all in this if condition, Inshort how to detect whether the given get request value is invalid or not..
Any help will be much appreciated.
.htaccess is the way to do this.
ErrorDocument 404 index.php?404
that line will tell apache what file to load. The example above calls the main index.php script.
add something like this to the top of your index.php file:
$error_404 = isset($_GET["404"]) ? true : false;
now you can detect if you have a 404 error request. $error_404 will be true, so why not add a simple function:
function error_404($error_404)
{
if($error_404 == true)
{
// do some error stuff here, like set headers, and some text to tell your visitor
}
}
now just call your function:
error_404($error_404);
best to do that immidiatley after the get handler:
error_404($error_404)
$error_404 = isset($_GET["404"]) ? true : false;
or combine the two into one line:
error_404($error_404 = isset($_GET["404"]) ? true : false);
to address the question, add this to the relevant script:
$pocategorys_ar = array("php","javascript");
if (!in_array($pocategory, $pocategorys_ar))
{
error_404(true);
}
Make sure it has access to the error_404() function.
You could put all categories inside an array like this:
$pocategories = array
(
'php',
'javascript'
);
if (in_array($pocategory, $pages))
{
// ...
}
else
{
header('Location:404.php');
}
Another thing you could do is creating a html/php file for every category and do it like so
if (is_file('sites/' . $popcategory . '.php')
{
include('sites/' . $popcategory . '.php');
}
else
{
header('Location:404.php');
}