wp_delete_post() force deleting post, override ignored - wordpress

I have set up a custom post type with a way to manage the posts on the front end of the site. On the front end users have the option to edit or delete posts.
When the user clicks delete, I run the wp_delete_post() function, to send the post to the trash. I don't want the post to bypass the trash, I would actually like the post to be sent to the trashcan.
The second parameter of the wp_delete_post is a boolean, true/false , which is supposed to dicate weather or not to bypass the trash.
The first is the ID of the post you'd like to delete.
http://codex.wordpress.org/Function_Reference/wp_delete_post
Here is my function:
wp_delete_post( $cpt_id, false );
As you can see, I've set the second parameter to false, but it still bypasses the trash and gets deleted from the server.
Is this a bug that I should report to core, or am I missing something simple?
Thanks!

Since you never want to delete and always want to trash, you could use wp_trash_post() instead. (http://codex.wordpress.org/Function_Reference/wp_trash_post)
However, that simply calls wp_delete_post() for you.
If none of your deletes are going to the trash, make sure that EMPTY_TRASH_DAYS is not set to 0 somewhere. (e.g. your wp-config.php) (http://codex.wordpress.org/Editing_wp-config.php)
HTH,
=C=

Related

Check condition before changing post status in wordpress

I have a form in the front end which enables users to submit posts. These posts are by default saved as pending. It can only set to publish by the admin. Before publishing certain conditions are to be checked before its set as published.
If the condition is true set post status as publish. If not set it as pending. What is the appropriate action hook to achieve this?
look here, there is 6 hooks than you can use for that :
https://core.trac.wordpress.org/browser/tags/5.3/src/wp-includes/post.php#L4058
just take care that the values that you use in your condition can also be change on this hooks then you have to test where you have the values you need.

Run checks on drupal_goto function

I have a strange issue with a drupal site and a custom form. I have a custom module that puts a form as a block...simple input field that posts a search query to a store locator.
The form keeps getting overridden and the action/url changes randomly at random times. I need a quick solution to this whilst we debug the real issue that's causing this.
I need to write an if statement that checks the drupal_goto function that goes along the lines of if drupal_goto == /store-locator then run else change the drupal_goto action to /store-locator.
Any help would be appreciated.

Wordpress Loop get_the_id()

I tried the following functions in header.php, footer.php, single.php etc.
var_dump(in_the_loop());
var_dump(get_the_id());
The first function gives false (meaning we are not in the loop) and the second function gives the post id every single time.
Description of get_the_id() from wordpress :
Retrieve the numeric ID of the current post. This tag must be within The Loop.
I just want a simple explanation what the hell is going on why do i get the post id if I call the function out of the loop !?
must is a little strong for get_the_id() ...delivers evil eye to Wordpress.
It works in the header and non-loop (confirmed).
Please note that post/page are essentially interchangeable in this conversation.
Think of WP this way -> You always have a post id in some way, all the time, every page, unless you do weird stuff or talk about non-page edge cases. When you are at the install root (such as site.com/) there are posts being called, something has to be displayed. There are other settings that will impact post/page such as static front page settings. On a category listing, if there are pages, I got the first ID returned before the loop.
On post/pages the page ID is (more or less0 set before the loop. This is a result of the URL (pretty or ?p=123 format) dictating the content. Using pretty names, the page at site.com/foo-bar/ will try to look up if there is content available via the permalink rules for "foo-bar". If there is content, the post ID is obtained. (simplified)
Later in the page build you get into the loop. However, before the loop you are also offered opportunities to change, sort, or augment the loop - such as changing the page IDs to be looped or sorting.
Regarding in_the_loop(), WP says
"True if caller is within loop, false if loop hasn't started or has ended." via http://codex.wordpress.org/Function_Reference/in_the_loop
in_the_loop() evaluates if the loop is in action (loop being key to the WP world). Also important - when you are in the loop WP can iterate over multiple page/post (IDs).
I don't have a 100% bulletproof explanation as to how the ID always shows, but when you dig into the API and various methods for hooking this might be a result.
I understand your confusion and agree with you. I think WP intended get_the_id() as a loop based tool, outside the loop you will get unpredictable results.
Hope that helps, I do enjoy working in WP, and I hope you do to.

Wordpress session management

I'm putting up a site using Wordpress and I'd like to piggyback on its sessions. But I'm not finding any plugins, or even documentation. Any suggestions or references before I start hacking it?
Note: I'm asking about if and how WP uses standard PHP sessions itself, not how to add PHP sessions e.g. using session_start(). Apparently any state WP maintains is accomplished by other means. So if I want to use PHP sessions I need to add and maintain it myself entirely, using techniques like those in the thread.
Thanks all!
It's a very bad idea to modify WP Core files for the ability to use sessions. The best way I've found is to call the session_start() from init action hook.
function kana_init_session() {
session_start();
}
add_action('init', 'kana_init_session', 1);
You can place it in functions.php file of your theme.
Detailed article can be found here: http://www.kanasolution.com/2011/01/session-variable-in-wordpress/
WordPress doesn't appear to call session_start() because it wants to be stateless
and if register_globals is defined, it automatically destroys your $_SESSION
Consider using WordPress Transient API
Values stored using the Transient API are visible to all users, not just the current user, depending on the unique identifier used to retrieve the transient, you could assign each user a unique identifier essentially causing a transient to behave very much like a session.
Further considerations:
Depending on a users setup with object cache, etc., transients may
not always be stored in the DB (e.g. memcached), using transients for
sessions could mean that the data can get bulky and fill memory
quickly (in the use of memcached).
Also, it seems that WP does not do auto garbage collection for
transients:
https://wordpress.stackexchange.com/questions/6602/are-transients-garbage-collected
For what I need to do, the best answer involves:
To allow the cookie for wordpress to persist across subdomains, install the Root Cookie plugin.
sub1.domain.com has wordpress; sub2.domain.com is another site. From the other site (sub2), I read the cookies to identify who the user is and if the user is logged in.
My cookies are as follows:
[wordpress_909bb230b32f5f0473202684d863b2e0] => mshaffer|1255298821|d0249fced9c323835c5bf7e84ad3ffea
[wordpress_logged_in_909bb230b32f5f0473202684d863b2e0] => mshaffer|1255298821|56e9c19541ecb596a1fa0995da935700
Using PHP, I can loop over the cookies, parse the key=>value pairs. These cookies let me know that [mshaffer] has a cookie stored on wordpress, and also is authenticated as logged_in. The expiry of the cookie is 1255298821.
In sub2, I can query the database of wordpress and grab the user info:
SELECT * FROM `wp_users` WHERE user_login = 'mshaffer' ... grab user_id, user_email from this query
SELECT * FROM `wp_usermeta` WHERE user_id = '$user_id' ... grab lots of other data from wp
With this info, I can add to my sub2 session variable / cookie and do what I want with the data. I can identify if I am logged in, and my username ... which let's me grab lots of different data. I can now use WordPress authentication in my sub2.domain.com and redirect accordingly.
monte
{x:
Wordpress doesn't seem to use any sessions.
The best way to go about it is to use the action hooks it provides.
Have you checked the solution here this may work for here and its on easy way
http://thedigilife.com/wordpress-how-to-set-session-custom-variable-while-login/
Hooking a function with session_start() on wp_loaded seems to work in this case.
Put this code in wp-config.php at first line:
if (!session_id()) {
session_start();
}
Put this code in theme's header.php at first line:
session_start();
Then it will maintain all session variables.
If you wanna use your own session values, Wordpress does support it.
You need to add following lines at the top of wp-config.php
if (!session_id()) {
session_start();
}
Then add following line at the top of header.php
session_start();

change user_profile_form form fields order

When a user login , the user will be redirect to a user profile page, which has a My account field set.
the field set has 2 fields, "Username: ", "Email address:". those 2 fields are generated by drupal.
those 2 field contained in a form which has a id ("user_profile_form") . I want to change the order of those 2 fields.
I have tried to intercept 'user_profile_form' , inside hook_form_alter.
code as follow:
$form['account']['name']['#weight'] = 1;
but that did not success, drupal did not even rendering the 'name' field, so no username: showed on browser.
What you did is absolutely correct, and probably did work. You can change the weight of the fields with the method described above.
The username field is not always rendered. The reason is that a persmission is required: change own username. If that perm is not set, you wont be allowed to alter you username and the field wont be shown.
Info on debugging.
Your info alone is not quite enough to debug. From what you describe, you are doing the right thing, but other modules could be making things a bit tricky for you. The devel module is quite good when it comes to debugging, ti defines two functions I use a lot when debugging:
dpm() pretty prints the variable to the message area using krumo.
dd() Prints / saves a variable to a log file. Useful when you can't view messages on the screen.
I would suggest that you look at the $form variable before and after you alter it.
Things that could make it go wrong:
Did you remember to pass the $form variable by reference using the & notation?
Is another module altering your form after you?
Are you checking for the correct form id, so you alter the correct form?
These are some pointers, before you bring more info, all I can do is guess to what your problem exactly can be. I did something like this a few days ago so I know what you describe shouldn't be a problem.

Resources