I am trying to figure out how to abort a running WordPress function. When the user deletes a custom post type (in my case, a store), I want to check to see if there are any associated posts with that store. I am running a query and checking to see if there are returned results. If we return 1 or more results, I want to abort the delete and present the user with an error message stating that they must delete the associated post. I am using the action 'before_delete_post'. Here is what I'm going for:
if (count($results)==0){
//delete the data
} else {
//abort the delete.
}
Thanks in advance for the assistance.
if you are using before_delete_post you could have something like this:
function prevent_delete_custom_post() {
if (count($results)==0){
//delete the data
} else {
wp_redirect(admin_url('edit.php')); //here you can try to get the variables that you have in the url to redirect the user to the same place.
exit();
}
}
add_action('before_delete_post', 'prevent_delete_custom_post', 1);
Remember that 'before_delete_post' action is fired before post metadata is deleted.
http://codex.wordpress.org/Plugin_API/Action_Reference/before_delete_post
And 'delete_post' action is fired before and after a post (or page) is deleted from the database.
http://codex.wordpress.org/Plugin_API/Action_Reference/delete_post
Related
I am building my wedding website and want to integrate an RSVP form using Gravity Forms. The issue I am running into is how to set certain guest that have +1's. I would like to show an additional guest entry (First Name, Last Name, Meal Option) when the initial First Name and Last Name has been populated. How would I go about doing this? Any help would be great! Thanks in advance!
Here is how I'd solve this problem:
First, you need to put everything in the DB, the easiest way would be to either do it manually or somehow loop through an array/CSV calling add_option($key, $value) Again, I would recommend a mobile/phone number as they'll be unique so you don't pull the wrong "John Smith". I'll assume you'll keep it basic with $key as the unique identifier and $value as boolean as to whether to show additional info. Interestingly, by default, if not found get_option($key) will return false and therefore not show your additional data, which I would assume you'd want anyway. If you'd rather it return true just pass true as the second argument.
Now for your answer:
Your URL is something like https://somesite.com/rsvp?id=1234.
function allowed_plus_one() {
$id = $_GET["id"];
$allowed = get_option($id);
return $allowed;
}
Then assumedly it'll be something like
if (allowed_plus_one()) {
// show form with plus one
} else {
// show form without
}
EDIT:
Keeping separate incase this has already been viewed.
You should also be checking for the existence of $_GET["id"] and behaving accordingly. eg:
if (isset($_GET["id"] && !empty($_GET["id"]) {
//do logic above
} else {
//here by mistake so don't show any form?
}
Form.php(controller)
public function dispdata()
{
$result['data']=$this->Form_model->displayrecords();
$this->load->view('display_records',$result);
if (!$this->session->userdata('ci_session'))
{
redirect('Form/login');
}
else
{
$this->session->set_tempdata('item',$result,5);
}
}
display_records(view)
<?php
if($this->session->tempdata('item')){
redirect('Form/login');
}
?>
im trying to work with the tempdata concept. i have a registration form where i have stored all the registered details in the datbase and those store details of database i have displayed
it in the view page.
how that i have displayed all the database details in a view page that view page im trying to display only for 5sec and after 5sec it should redirect to the login page. i have tried with the above code but its not working please can anyone tel me where im going wrong ?
The tempdata feature for sessions only affects how long the data is allowed to be stored. The removal of the data, after 5 seconds in your case, won't cause anything else to change on the page.
As far as I can tell you don't need tempdata at all. Try this to see if you get the behavior you want.
public function dispdata()
{
if (!$this->session->userdata('ci_session'))
{
redirect('Form/login');
}
$result['data']=$this->Form_model->displayrecords();
$this->load->view('display_records',$result);
// sleep for 5 seconds
sleep(5);
redirect('Form/login');
}
Why did I remove the else from your code? Because redirect does not return - it ends script execution. So, when the if evaluates to true and the redirect executes this script is done.
Currently, I am having one page that display query options and does the form validation and another page that process the query and shows the result if validation is successful. I am trying to combine these two pages together such that the user would not need to go back and forth the two pages every time to make some query changes.
The structure of the two page process is as follows:
**Validation Page**
if (post detected)
{
validate input
if (no error)
{
record query options
redirect to results page
exit
}
else
{
output error message
}
}
display form
**Results Page**
if (query options are set)
{
process query
display results
}
else
{
redirect to validation page
}
I have seen the concept being implemented simply in search engine pages where the search box and the results are in one page. I am looking to implement something like that using the POST method with a form containing both select and input boxes.
You can just set the form action to itself (or leave it blank along the lines of action = "" and it will point to itself anyhow, then use a simple check to see whether any form data has been submitted to determine if you should show the empty page or the search results:
if(isset($_REQUEST['searchStuffs']) // Not sure if you are GET or POST'ing
{
if(!empty($_REQUEST['searchStuffs'])
{
// do stuff here to get the form result, then display it
}
else
{
// The form was submitted empty, so show an error
}
}
else
{
// Display the normal search/form page as it hasn't been sent.
}
You can also use the following approach (which I would probably use though it is some extra work):
$searchBool=false;
if(isset($_REQUEST['searchStuffs']) // Not sure if you are GET or POST'ing
{
if(!empty($_REQUEST['searchStuffs'])
{
if(...)// valid search criteria
{
$searchBool=true;
// Do stuff to get search results
$searchResults='some output or data';
}
}
}
// Echo normal input form
if($searchBool)
{
echo $searchResults;
}
I have a activate button in my CMS that allows the user to quickly toggle a active/inactive flag for news articles. When clicked the toggle makes a POST request to the url /news/toggle_active/$id, after which the news_model updates the boolean variable in the database for the specific articles id, and the controller redirects back to the index.
Controller
function toggle_active()
{
$url = $this->uri->uri_string();
$last_segment = count($this->uri->segment_array());
$id = $this->uri->segment($last_segment);
$row = $this->news_model->set_active($id);
redirect('news/index', 'refresh');
}
Model
function set_active($id)
{
$data = array(
'display_flg' => 'NOT display_flg'
);
$this->db->where('id', $id);
$this->db->update($this->tbl_news, $data);
}
The first time the activate button is clicked it works, but subsequent clicks do nothing. If I close the browser window and re-open the page, the button works again, but only for one click.
I haven't specifically enabled any caching, so I'm not sure whats going on.
If I remove the redirect from the end of the toggle_active() method, and output the current value of the display_flg variable, and POST to /news/toggle_active/$id it appears to do nothing on the first request. However on each subsequent refresh the variable changes values. Why is it not doing anything the first time the link is hit?
I found some people with what seems to be a similar issue over at the codeigniter forums, although there was no solid solution.
I don't think this was a caching problem in the end. I made two major changes to fix the problem.
Firstly, I not check to see whether or not the database request was successful before redirecting.
I also re-wrote my the models set_active() method. Passing the SET clause as an array to the update functions second parameter wasn't working. I needed to use the set functions third parameter to stop the query from escaping the NOT part of the query.
I adjusted my functionality as follows:
Controller
function show_toggle($id)
{
if($this->news_model->set_active($id))
{
redirect('news/index', 'refresh');
}
}
Model
function set_active($id)
{
$this->db->where('id', $id);
$this->db->set('display_flg', 'NOT display_flg', FALSE);
$query = $this->db->update($this->tbl_news);
return $query;
}
I have a relatively simple class which deletes a post:
function delete_post($postid, $reason){
//Stuff to delete post
$this->delete_response = 'Thanks, your course has been removed.';
}
This function is called at the top of a page with a form on. If the form is submitted, the same page checks the POST[] and carries out the function, like so:
if(!empty($_POST['removecourse'])){
$courseManager->delete_post($_POST['courseid'], $_POST['cancel-reason']);
echo $courseManager->delete_response;
};
So my problem is... when I refresh the page, the message keeps displaying. I know this is because I am re-submitting the form, and because there is no such P/R/G pattern going on, but as i am new to OOP, im wondering if im doing this the right way, or if anyone could suggest a way similar to PRG or something?
Add an if that test if somthing changed, like mysql_affected_rows
function delete_post($postid, $reason)
{
//Stuff to delete post
if(mysql_affected_rows())
{
$this->delete_response = 'Thanks, your course has been removed.';
}
}