以下是我遵循的流程。
第1步。指定要放置页面的页面的slug。为了避免将其硬编码到插件中,我添加了一个设置选项来存储页面的slug。因此,如果我更改页面,我只需在设置页面中指定新页面的slug。
function my_plugin_settings_page() {
//Add a settings page for this plugin to the Settings menu.
add_options_page( \'My Plugin Settings\', \'My Settings\', \'manage_options\', \'my-plugin-settings\', \'display_my_plugin_settings\' );
}
function display_my_plugin_settings{?>
<div class="wrap">
<h2><?php echo esc_html(get_admin_page_title()); ?></h2>
<?php
$options = get_option(\'my-plugin\');
settings_fields(\'my-plugin\');
?>
<form method="post" name="my-plugin-settings" action="options.php">
<fieldset>
<legend class="screen-reader-text"><span><?php _e(\'Signup Page\', \'my-plugin\'); ?></span></legend>
<label for="my-plugin-signup">
<?php echo home_url(\'/\');?><input type="text" id="my-plugin-agent_signup" name="my-plugin[signup]" value="<?php echo $options[\'signup\'];?>"/>
<span><?php esc_attr_e(\'Signup Page\', \'my-plugin\'); ?></span><br>
<span class="description"><?php esc_attr_e(\'The signup form will be added to the end of this page\', \'my-plugin\'); ?></span>
</label>
</fieldset>
<?php submit_button(\'Save\', \'primary\',\'submit\', TRUE); ?>
</form>
</div>
<?php}
add_action( \'admin_menu\', \'my_plugin_settings_page\' );
第2步。您必须更改要在该页面上使用的模板。为此,我们使用挂钩
page_template
.
public function signup_template($page_template) {
$options = get_option(\'my-plugin\'); //get plugin options
if (is_page( $options[\'signup\'] )) {
$page_template = dirname( __FILE__ ) . \'/public/partials/registrationform.php\';
//change the template if the page is the one specified
}
return $page_template;
}
add_filter( \'page_template\', \'user_profile_template\', 11 ); //filter hook to change the page template