将 HTML 输入插入主题模板文件?
我的问题是:
目前使用我的代码,我可以让我在自定义设置页面中设置的任何内容出现在 当然,每个帖子的结尾都使用 WordPress 过滤器。
唯一的问题是,我不希望我输入的内容出现在帖子中的任何地方。我想做什么
实现,就是将我的输入注入(inject)到当前主题的home.php模板文件中,在一个<div></div>
中。该模板文件中包含的标记。 <div>
有问题的附加了一个 ID,即 <div id="category-inner">
,那么有什么方法可以使用它的 ID 在我的插件中定位它吗?
我已经成功地做到了,通过编辑实际的 home.php 模板文件并直接在其中插入一些 php 来显示用户输入,但这完全违背了我正在尝试做的事情,即不必编辑模板文件的源代码,只需使用我的插件将用户输入的文本插入到该特定位置(上面提到的 <div>
)。
我的插件只会在网站的一个地方添加用户输入,而且永远不会改变。它总是会进入的地方,就是我上面提到的地方。
下面是我的插件代码:
<?php
/*/
Plugin Name: Custom Text Adder
Plugin URI: N/A
Description: Demonstrates how rubbish I am at pretty much everything I want to do
Version: 101
Author: JD
Author URI: N/A
/*/
// insert custom plugin settings menu
add_action('admin_menu', 'custom_create_menu');
add_filter('the_content', 'customhead_the_content');
function customhead_the_content($content) {
$output = $content;
$output .= '<div id="category-inner">';
$output .= get_option('post_text');
$output .= '</div>';
return $output;
}
// Add Font-Size to WYSIWYG Editor
function wp_editor_fontsize_filter( $options ) {
array_shift( $options );
array_unshift( $options, 'fontsizeselect');
array_unshift( $options, 'formatselect');
return $options;
}
add_filter('mce_buttons_2', 'wp_editor_fontsize_filter');
// Create Custom Menu
function custom_create_menu() {
//create new top-level menu
add_menu_page('Custom Plugin Settings', 'Custom Settings', 'administrator', __FILE__, 'custom_settings_page',plugins_url('/img/icon.png', __FILE__));
//call register settings function
add_action( 'admin_init', 'register_mysettings' );
}
// Register our settings
function register_mysettings() {
register_setting( 'custom-settings-group', 'new_option_name' );
register_setting( 'custom-settings-group', 'some_other_option' );
register_setting( 'custom-settings-group', 'option_etc' );
register_setting( 'custom-settings-group', 'font_size' );
register_setting( 'custom-settings-group', 'post_text' );
}
function custom_settings_page() {
?>
<!-- Custom Settings Page Container -->
<div class="wrap">
<h2>Custom Text</h2>
<form method="post" action="options.php">
<?php settings_fields( 'custom-settings-group' ); ?>
<table class="form-table">
<?php /* Bring the editor onto the page */
wp_editor( '', 'post_text', $settings = array() );
// 4.
// Custom buttons for the editor.
// This is a list separated with a comma after each feature
// eg. link, unlink, bold, ...
$settings = array(
'textarea_name' => 'post_text',
'media_buttons' => true,
'tinymce' => array(
'theme_advanced_buttons1' => 'formatselect,|,bold,italic,underline,|,' .
'bullist,blockquote,|,justifyleft,justifycenter' .
',justifyright,justifyfull,|,link,unlink,|' .
',spellchecker,wp_fullscreen,wp_adv'
)
);
submit_button( 'Save everything', 'primary', 'submit' ); ?>
</table>
</form>
</div>
<?php }; ?>
下面是截图,可以用最简洁的方式解释这一点:
http://i.imgur.com/hiEjEsA.jpg
再次,非常感谢任何和所有帮助,并希望能阻止我的大脑受伤!
你们都很摇滚,
凯西。 :)
最佳答案
我不确定您是否希望这是一个纯 PHP 解决方案,但是由于您拥有 div 的 ID,您可以使用 jQuery 将其定位并在页面加载(或表单提交)时将文本插入到 div 中像这样:
$(document).ready(function() {
var userText = $('#input-id').val();
$('#category-inner').html(userText);
})
关于php - 通过 Wordpress 插件定位特定的 `<div>`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16068741/