Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If this is a plugin, you need to set the option for the first time with <a href="http://codex.wordpress.org/Function_Reference/register_activation_hook" rel="nofollow noreferrer"><code>register_activation_hook</code></a>. If a theme, use <a href="http://codex.wordpress.org/Plugin_API/Action_Reference/after_setup_theme" rel="nofollow noreferrer"><code>after_setup_theme</code></a>.</p> <p>There's an alternative technique for <code>run_once</code> <a href="https://wordpress.stackexchange.com/a/25667/12615">described in this WPSE Answer</a> that works quite well too.</p> <p>Here's a working example <sup>loosely based on yours and some Codex samples</sup> using <code>run_once</code>. Note the use of the function <a href="http://codex.wordpress.org/Function_Reference/checked" rel="nofollow noreferrer"><code>checked()</code></a>.</p> <pre><code>add_action('admin_init', function() { // Change the "init_*" to anything else to `run_once` again if ( wpse_25643_run_once('init_checkbox_default') ) { add_option('my_checkbox_default', true ); } register_setting('media','my_checkbox_default'); add_settings_field( 'my_checkbox_default', 'Checkbox:', 'my_default_checkbox_settings_function', 'media' ); }); function my_default_checkbox_settings_function() { printf( "&lt;input name='my_checkbox_default' id='gv_thumbnails_insert_into_excerpt' type='checkbox' value='1' class='code' %s /&gt; Explanation text", checked( 1, get_option('my_checkbox_default'), false ) ); } function wpse_25643_run_once( $key ) { $test_case = get_option( 'run_once' ); if ( isset( $test_case[$key] ) &amp;&amp; $test_case[$key] ) { return false; } else { $test_case[$key] = true; update_option('run_once',$test_case); return true; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload