$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>posts - Define a wordpress constant through plugin functions?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

posts - Define a wordpress constant through plugin functions?

matteradmin10PV0评论

I'm working on a plugin and trying to build in an option to disable post revisions. I have the setting registered, and confirmed that the checkbox is linking to the database. The option value is either null or 1.

What I want to do is define this constant: define('WP_POST_REVISIONS', false); in the plugin file only if the option is set to 1.

If I put the constant directly into the plugin functions file it works, but if I try and use it in an if statement or through a function, it doesn't work, no matter how I try and do it.

I'm getting the option value like this: $disable_revisions = get_option('disable-revisions');

Here's what I've tried:

if ($disable_revisions==1){
    define('WP_POST_REVISIONS', false);
}

I also tried using a seperate function:

if ($disable_revisions==1){
    add_action('admin_init', 'disable_revs');
}

function disable_revs(){
    define('WP_POST_REVISIONS', false);
}

And I tried adding the action to 'wp', 'init' and a few others too, but none of them work.

How can I define the constant only if $disable_revisions = 1?

I'm working on a plugin and trying to build in an option to disable post revisions. I have the setting registered, and confirmed that the checkbox is linking to the database. The option value is either null or 1.

What I want to do is define this constant: define('WP_POST_REVISIONS', false); in the plugin file only if the option is set to 1.

If I put the constant directly into the plugin functions file it works, but if I try and use it in an if statement or through a function, it doesn't work, no matter how I try and do it.

I'm getting the option value like this: $disable_revisions = get_option('disable-revisions');

Here's what I've tried:

if ($disable_revisions==1){
    define('WP_POST_REVISIONS', false);
}

I also tried using a seperate function:

if ($disable_revisions==1){
    add_action('admin_init', 'disable_revs');
}

function disable_revs(){
    define('WP_POST_REVISIONS', false);
}

And I tried adding the action to 'wp', 'init' and a few others too, but none of them work.

How can I define the constant only if $disable_revisions = 1?

Share Improve this question asked Jan 29, 2019 at 9:53 Nexus6Nexus6 232 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I don’t think you should define constants in your plugin. It will be very hard to debug later on.

IMHO using wp_revisions_to_keep filter will be much nicer solution.

So your code could look like this:

add_filter( 'wp_revisions_to_keep', 'my_revisions_to_keep_based_on_settings', 10, 2 );

function my_revisions_to_keep_based_on_settings( $num, $post ) {
    // change that according to your needs
    return intval( get_option('disable-revisions') );
}
Post a comment

comment list (0)

  1. No comments so far