« first day (1656 days earlier)      last day (3277 days later) » 

4:10 AM
To rename user roles all over my project I'm using this code snippet, and it's working fine. I changed default user role author to Data Entry Officer. It's visible in back end too (screenshot). but... where I'm displaying the user role like:
global $current_user;
echo $current_user->roles[0];
it's displaying author. :(
How can I solve that?
Actually I don't want to re-invent the wheel, and want to extend the default user roles to match up to my need.
 
 
5 hours later…
9:06 AM
It's inside WordPress so yes, PHP. :)
Without having looked at it what is better about http://symfony.com/doc/current/components/dom_crawler.html compared to https://php.net/manual/en/book.dom.php#book.dom
 
EVERYTHING
no, seriously, Dom Crawler wins PHP in "making horrible pain go away" category
 
Well in fact all I need is get all h2, add and id to them and return a list of anchors to those. Really worth using an extra lib for that?
 
you can use the lib now, or you can waste hours trying to rig it together reliably and use the lib then
 
@MayeenulIslam Use $wp_roles->get_names()
 
Okay, okay. Convinced.
 
9:46 AM
@toscho I understood. But that's not retrieving current users role.
 
@MayeenulIslam Use the current user's ID.
 
 
2 hours later…
11:59 AM
What happened to wp_list_categories() in WP 4.2? One of my client project simply doesn't showing any categories of default 'category' taxonomy! :(
 
Maybe term splitting got you? make.wordpress.org/core/2015/02/16/…
 
ohoo! Found some parameters may be vanished, like:
'selected', 'name', 'id', 'tab_index'
Just commented 'em out, and its came back. :)
 
 
3 hours later…
2:38 PM
Can anybody replicate this issue? It almost seems like my upload is being saved on a revision...

http://wordpress.stackexchange.com/questions/185839/custom-upload-adds-ghost-file
 
17 hours ago, by toscho
@Howdy_McGee wp_is_post_revision( $post )
 
Ah right, I meant to try that this morning. 1s
Well I guess it's not a post revision as I thought. I keep seeing auto-draft as post status though when uploading.
 
3:01 PM
Some how though I keep getting post revision ID being passed, if I get the post of the passed ID the title is something like 69-revision-v1 - how reliable is the passed post id supposed to be?
Wow, I guess that is the issue ! wp_is_post_revision( get_post( $post_id ) ) - thanks toscho
If trying to save on the revision though, what's the point of defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ?
Is that not needed anymore when saving post meta
 
@Howdy_McGee you should use wp_is_post_autosave( $post ) nowadays anyway.
 
Oh man, i'm behind the times then :/
so global $post more reliable than the passed post id?
 
/**
 * Determines if the specified post is an autosave.
 *
 * @since 2.6.0
 *
 * @param int|object $post Post ID or post object.
 * @return bool|int False if not a revision, ID of autosave's parent otherwise
 */
function wp_is_post_autosave( $post ) {
2.6.0 :D
 
lol
 
slightly behind
 
3:19 PM
If you don't mind me bugging you further :D - - - How do people normally do their post checks when saving post meta ( `save_post` hook ) ?

global $post;
if( ! ( isset( $post ) || wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) ) {
return;
}

?
 
never touch global
 
get_post( $post_id ) on the passed ID?
 
you get the post_id and the complete WP_Post object as parameters
 
Oh, I see looking at the save_post docs now.
 
3:38 PM
@Howdy_McGee you are aware that wp_insert_attachment() calls wp_insert_post(), so your save_post callback runs again?
 
:/ no I wasn't
 
always check the post type
 
Though now that you mention it, looking at the function I can see it's kinda ovboius...
Yeah, I check post types and nonces before going into any actual meta updating - that question just had stripped down code - the actual file validation is fairly long.
Thanks again @toscho - off to update my installs :S
 
can anyone get @cybmeta off my back wordpress.stackexchange.com/questions/185845/switch-vs-if-else/…? I don't mind him running around down voting my answers but the notification he generates with his poor excuses are annoying.
 
function wpse_save_post_meta( $post_id ) {

    // verify nonce
    if (!isset($_POST['cpt_wpse_nonce']) || !wp_verify_nonce($_POST['cpt_wpse_nonce'], basename(__FILE__)))
        return $post_id;
    // check autosave
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
        return $post_id;
    // check permissions
    if ( 'wpse' == $_POST['post_type'] ) {
        if ( !current_user_can('edit_page', $post_id) )
            return $post_id;
        } elseif ( !current_user_can('edit_post', $post_id) ) {
This is how I taught to do it @Howdy_McGee
 
3:53 PM
or is there any setting I can use to tatally ignore him?
 
@Howdy_McGee but as @toscho suggested here, we may replace if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id; with wp_is_post_revision( $post_id ) return $post_id;
 
@MarkKaplun if it happens again, flag the comment, and explain the issue in the flag comment.
@MayeenulIslam wp_is_post_autosave()
 
@MarkKaplun There's no @cybmeta in that post now
@toscho Oops!
 
And please do not return the post ID. That's not useful. return the reject reason instead, so you can run tests on that function.
 
@toscho got that. So it'd be simply wp_is_post_autosave( $post_id ) return; - nah?
 
4:02 PM
and so on …
if ( wp_is_post_autosave( $post_id ) )
	return 'autosave';

if ( wp_is_post_revision( $post_id ) )
	return 'revision';
 
@toscho :) Thanks
Can't we make a proposal to Area51 for emoji in this chat room?
 
veto
 
+1 veto
bad enough this kids-stuff is in core...
 
hahahaha :)
 
@toscho what are the odds though that it should fail? Isn't it easier to combine them all into one conditional and *if* you run into an issue break it out for debugging at that point?

if( ! isset( $post ) || wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
return;
}
 
4:15 PM
You can even use emojis on GitHub. But you cannot search in commit messages. Priorities …
2
@Howdy_McGee unit tests?
 
@toscho forget it. Let's talk about @Howdy_McGee's issue - actually it's my issue too - I'm also interested learning this.
 
@toscho - Right, normally it's just easier to combine it until something happens then break it up like so
 
It is easier to write testable code from the very beginning.
 
Well, while on that note F emjoiis
easier but bulky, that's 3 conditionals 9 lines + 2 blank spaces to break them up. Maybe it's just preference idk.
 
compactness is not a sign of good quality.
I move the validation usually to a completely different object
 
4:18 PM
What about readability? One sentence is easier to read than paragraphs
Maybe that's a poor comparison
@MayeenulIslam - I think the issue with my question relied on revisions, post types, and nonces, it was creating a fake attachment based on the revision because I wasn't checking any nonces or post types.
 
public function save( $post_id, \WP_Post $post ) {

	$status = $this->validator->status( $post );

	if ( Post_Validator::OK !== $status )
		return $status;

	// save the data
}
 
What does your validator actually validate though? Statuses and types?
Oh, status()
 
status() might return INVALID_NONCE, REVISION and so on.
 
Gotcha, that's a cleaner method.
 
 
5 hours later…
8:56 PM
public function save( $post_id, \WP_Post $post) {

    try {

       // throws an exception if bad status or any other reason
       $this->validator->validate( $post );

       // save data here

    } catch( \Exception $e ) {

        return new \WP_Error( $e->getCode(), $e->getMessage(), $e->getTrace() );
    }
}
@Howdy_McGee, @toscho
If the post is deleted by another user after validor validated this approach saves your day
 
9:42 PM
What's up with the backslashes?
 
You'll need them if you're using a class with namespaces
So you're ignoring the current namespace and use the absolute namespace instead
 
10:28 PM
I don't like exceptions. they make the code harder to read.
 

« first day (1656 days earlier)      last day (3277 days later) »