Prevent same post value WordPress - php

I need to stop inserting in the WordPress database a value if already exists. What I tried and I don't know why it's not working. I'm using the next code in functions.php and I have and ajax call. Can someone make some light on this please.
My code
$i_can_add_value = true;
$error_reason = false;
$published_values = $wpdb->get_results(
"SELECT ID FROM ".$wpdb->posts." WHERE post_author = %d AND post_status IN ('draft','publish') AND post_type = 'custom' ", $current_user->ID
);
if (!empty($published_values)){
foreach ($published_values as $id_already_published){
$temp_custom = get_post_meta($id_already_published->ID, 'custom', true);
if ($temp_custom === $description && $description !='') {
$i_can_add_value = false;
$error_reason = 'You already added that!';
}
}
}
if($i_can_add_value){
wp_insert_post($my_post);
} else {
echo "Error: ".$error_reason;
}

First of all you need to be sure that you code is working at all when ajax is called.
If this code is working then you need to check three conditions in your code.
1) if (!empty($published_values)
$published_values can be empty because of the incorrect sql query.
2) $temp_custom
Can also be empty. Check it.
3) $description
And this variable can be empty as well.

Related

PHP Not Running On Page Until Refresh

bit of a strange one that I've not been able to resolve for months so I finally have given in and have come here for the answer. Hopefully.
I have the below shortcode that when ran returns the phone number depending on what variable has a value. This PHP code works as expected, the one thing that doesn't work as expected however, is the first ever page load.
When someone goes to the site for the first time (or in incognito mode) the shortcode doesn't output anything, however, refresh the page just once and it'll display the output of the correct value and I have no idea why.
<?php function gw_cookie($atts) {
extract(shortcode_atts(array(
"value" => ''
), $atts));
$getVariable = isset($_GET[$value]) ? $_GET[$value] : '';
$newGetVariable = str_replace('_', ' ', $getVariable);
$cookiePhone = isset($_COOKIE[$value]) ? $_COOKIE[$value] : '';
$acfField = get_field('page_cookie_'.$value.'');
$optionsACF = get_field('options_company_details', 'options');
$area = $optionsACF['options_area'];
$phone = $optionsACF['options_telephone'];
if(!empty($cookiePhone) && $cookiePhone !== 'undefined') { //If cookie is not empty do the following
echo '1';
} elseif(!empty($newGetVariable)) { //If cookie is empty and get variable is not
echo '2';
} elseif($acfField) { //If ACF field is not empty do the following
echo '3';
} elseif ($value == "phone") {
return '4';
}
} add_shortcode("gw-cookie", "gw_cookie");
This codes file is being imported into the functions.php file using the following line:
require_once( __DIR__ . '/shortcodes/gw-cookies.php' );
A cookie itself would be created on the first run and your criteria requires cookiePhone which is why you have to refresh to make it work.
As per the comments, change:
$cookiePhone = isset($_COOKIE[$value]) ? $_COOKIE[$value] : '';
to:
$cookiePhone = isset($_COOKIE[$value]) ? $_COOKIE[$value] : NULL;

PHP write a conditional statement based on whether an argument is in a database or not

I am developing a wordpress plugin. There is a variable that I send to the database once a payment has been made, that is ispayment and it is set to 1. If the payment has not been made, then the variable is not sent to the database...so there is an instance where, for a particular post ID, the variable "ispayment" does not exist.
I'm now trying to write a conditional statement based on whether this variable is present or not.
What I'm trying to say is, if wedding_form_final_submit = 1 AND there is no ispayment variable - $status = foo. Else, if wedding_form_final_submit = 1 AND ispayment = 1, $status = bar.
Here is what I have so far:
if(get_post_meta($post_id,'wedding_form_final_submit', 1) && get_post_meta($post_id, 'ispayment',false)) {
$status = 'Form Complete';
} elseif(get_post_meta($post_id,'wedding_form_final_submit', 1) && get_post_meta($post_id, 'ispayment',true)) {
$status = 'Deposit Paid';
} else {
$status = '';
}
Currently, this just returns "Form Complete" but it's for an entry that should return Deposit Paid, the entry that should say Form Complete is just blank.
I believe my issue is that for my first statement, the variable ispayment doesn't exist. How do I ammend this though?
This code below will probably do, if the post meta you're retrieving doesn't exist, your variable just return empty
// get post meta value 'ispayment'
$paid = get_post_meta( $post_id, 'ispayment', true );
//Check where post meta exists and value is 1
if ( isset($paid) && $paid === '1' ) {
$status = 'Form Complete';
//If post meta doesn't exist or value is not 1.
} else {
$status = 'Deposit Paid';
}
//or shorter
$paid = get_post_meta( $post_id, 'ispayment', true );
$status = ( isset($paid) && $paid === '1' ) ? 'Form Complete' : 'Deposit Paid';
just add your other code in there

php check empty or null in many variables

Hello I have a simple script
This is my script, and try this script
<?php
$user_id = $_REQUEST['user_id'];
$pid = $_REQUEST['pid'];
$nopr = $_REQUEST['nopr'];
$tglpr = $_REQUEST['tglpr'];
$uraianpr = $_REQUEST['uraianpr'];
$jenispr = $_REQUEST['jenispr'];
$nilaipr = $_REQUEST['nilaipr'];
$costproject = $_REQUEST['costproject'];
$remarkpr = $_REQUEST['remarkpr'];
$tmpattachid = $_REQUEST['tmpattachid'];
if ($user_id==NULL)
{
$error "User Id Not Complete";
}
else if ($pid==NULL)
{
$error= "PID Not Complete";
}
echo $error;
?>
I want if $user_id or other variables is empty/null
i confused when other variable have empty/null data
i must typing code many if statement :(
example : 5 variables ($pid,$nopr,$tglpr,$jenispr,$costproject) is empty/null
this show error like this
PID Not Complete
NoPR Not Complete
Tgl Pr Not Complete
Jenis Pr Not Complete
Cost Project Not Complete
Help Me, Thank's.
I won't give you the full answer, as stated in comments, this is a basic basic concept you need to learn, but I will show you the template.
You want to use a FOREACH loop to check each value in the array. See http://php.net/manual/en/control-structures.foreach.php
And Set it out like this:
foreach($_REQUEST as $key=>$row){
if (is_null($row) || empty($row)){
$errorText .= "You have no data in your ".$key."<br>";
}
}
unset($key,$row);
Then you can output the $errorText value telling people which rows should be fixed. Easy.
Loop through the $_REQUEST and find the empty or null key.
CODE :
foreach ($_REQUEST as $key => $value) {
if(trim($value) ==='' || is_null($value))
{
echo $key . " is not complete" . "<br/>";
}
}

Update PHP for AiContactSafe to add multiple redirects dependent on Combobox Value

This may help a few people using Aicontactsafe with joomla as the core component does not have it currently as an option. Essentially i need to change the module php file to allow different redirect urls depending on what dropdown option is selected in the "Studio" Combobox field.
Perfectly honest with you i do not know if i am typing in the correct fields or code values:
The test site you can see here - http://www.datumcreative.com/wellness/ and the form is 'book my taster class' on the right.
Below is where i believe code needs adding in the original file:
if (!array_key_exists('return_to', $parameters)) {
// initialize the database
$db = &JFactory::getDBO();
$query = 'SELECT redirect_on_success FROM #__aicontactsafe_profiles WHERE id = ' . $pf;
$db->setQuery( $query );
$redirect_on_success = $db->loadResult();
// set the return link to the current url or the one setup into the profile
if (strlen(trim($redirect_on_success)) == 0) {
$postData['return_to'] = $currentUrl;
} else {
$postData['return_to'] = $redirect_on_success;
}
}
To create the other redirects i added the following custom lines, below the line "$redirect_on_success = $db->loadResult();":
$redirect_to_wgc = "http://www.google.com";
$redirect_to_harp = "http://www.yahoo.co.uk";
I then needed to add the "if" statements **.
**if ($field_values['aics_studio'] == "Welwyn Garden City") {
$postData['return_to'] = $redirect_to_wgc;
}
elseif ($field_values['$aics_studio'] == "Harpenden") {
$postData['return_to'] = $redirect_to_harp;
}**
elseif (strlen(trim($redirect_on_success)) == 0) {
$postData['return_to'] = $currentUrl;
} else {
$postData['return_to'] = $redirect_on_success;
}
No luck so far, any suggestions?

PHP/SQL syntax: passing variable to SQL query

I am getting tired trying to see what is wrong. I have two php. From the first I am sending a variable 'select1' (basically the id) to the second and than I want to update that record uploading a pdf file.
$id = "-1";
if (isset($_GET['select1'])) {
$id = mysql_real_escape_string($_GET['select1']);
}
if(isset($_POST['Submit'])) {
$my_upload->the_temp_file = $_FILES['upload']['tmp_name'];
$my_upload->the_file = $_FILES['upload']['name'];
$my_upload->http_error = $_FILES['upload']['error'];
if ($my_upload->upload()) { // new name is an additional filename information, use this to rename the uploaded file
mysql_query(sprintf("UPDATE sarcini1 SET file_name = '%s' WHERE id_sarcina = '%s'", $my_upload->file_copy, $id));
}
}
If I put a line with a valid id, like:
$id = 14;
it is working. What I am doing wrong? Thank you!
If you need to accept both post & get, then you should try something like the code below to retrieve the variable.
$var = 'select1';
if( isset( $_POST[$var] ) ) {
$id = $_POST[$var];
} else if( isset( $_GET[$var] ) ) {
$id = $_GET[$var];
} else {
$id = -1;
}
You are using both GET and POST at the same time. As far as I can see, this condition is not returning True
if (isset($_GET['select1']))
Edit: If you don't find any answer in above; maybe some more information/code can help getting to a solution.

Categories