你可能只是错过了add_settings_section
之前add_settings_field
. 这样可以工作,并将字段保存到option
, 但我可能会扣上那部分。我只是想在登机前就上钩options-permalink.php
.
<?php
// ADD SETTINGS
add_action(\'admin_head\', function(){
// Create a section after the permalink
add_settings_section(
\'permalink_relative_setting_section_options\', // id
__( \'Protocol Relative URL\', \'textdomain\' ), // title
\'premalink_relative_setting_section_callback_function\', // callback
\'permalink\' // page
);
// Add a field to the section
add_settings_field(
\'permalink_relative_settings_field-id\', // id
\'\', // title
\'permalink_relative_settings_field_callback\', // callback
\'permalink\', // page
\'permalink_relative_setting_section_options\', // section
array( \'some_arg\' => \'For use in the callback\' ) // args
);
});
// SECTION
function premalink_relative_setting_section_callback_function( $args ) {
// Echo SECTION intro text here
/*
echo \'<p>id: \' . esc_html( $args[\'id\'] ) . \'</p>\'; // id: eg_setting_section
echo \'<p>title: \' . apply_filters( \'the_title\', $args[\'title\'] ) . \'</p>\'; // title: Example settings section in reading
echo \'<p>callback: \' . esc_html( $args[\'callback\'] ) . \'</p>\'; // callback: eg_setting_section_callback_function
*/
}
// FIELD
function permalink_relative_settings_field_callback( $args ) {
$options = get_option( \'permalink_relative_setting_section_options\' );
$html = \'<input type="checkbox" id="relative_checkbox" name="permalink_relative_setting_section_options[relative_checkbox]" value="1"\' . checked( 1, $options[ \'relative_checkbox\' ], false ) . \'/>\';
$html .= \'<label for="relative_checkbox">Do not apply to external links</label>\';
echo $html;
}
// UPDATE
add_action( \'init\', function() {
// If permalink structure is detected, let\'s update our option before the page can act
if ( isset( $_POST[ \'permalink_structure\' ] ) || isset( $_POST[ \'category_base\' ] ) ) {
check_admin_referer( \'update-permalink\' );
$options = get_option( \'permalink_relative_setting_section_options\' );
$options[ \'relative_checkbox\' ] = ( isset( $_POST[ \'permalink_relative_setting_section_options\' ][ \'relative_checkbox\' ] ) && $_POST[ \'permalink_relative_setting_section_options\' ][ \'relative_checkbox\' ] === \'1\' )
? \'1\' // checked
: \'\';
update_option( \'permalink_relative_setting_section_options\', $options );
}
} );
如果您查看
https://github.com/WordPress/WordPress/blob/master/wp-admin/options-permalink.php#L178 没有任何钩子可以做你想做的事。
考虑jQuery的manipulation 方法,如detach
和insertAfter
您可以在admin_footer
.
jQuery(\'<h1>Protocol Relative URL Form!</h1>\').insertAfter(\'.form-table.permalink-structure\');