当前位置:
  1. 首页 »
  2. 教程 »
  3. 正文

wordpress插件开发 添加设置选项

零分 2,403

在开发插件是,需要有设置选项,对一些参数进行设置。

百度了很多(不知道为什么,现在有用的资源都不好找到了)

很多以前的博客网站也都不更新了,有的资源也基本都是收费,且不说费用多少,在获得资源之前,也不知道是不是自己需要的资源,对于很多代码,接口,API等也只能是通过一些片段来测试,研究,组合,最后拼凑出自己需要的,完整的来!

在插件里添加设置链接

add_filter('plugin_action_links', 'add_Lf_Password_Access_css_link', 10, 2 );
function add_Lf_Password_Access_css_link($links, $file) {
	static $this_plugin;
	if (!$this_plugin) $this_plugin = plugin_basename(__FILE__);
 
	if ($file == $this_plugin){
		$settings_link = '<a href="'.wp_nonce_url("options-general.php?page=Lf_Password_Access").'">设置</a>';
		array_unshift($links, $settings_link);
	}
	return $links;
}

效果:

添加设置页面:

/**插件设置选项**/
add_action('admin_menu', 'Lf_Password_Access_setup_menu');

function Lf_Password_Access_setup_menu() {

  add_options_page(
    __( '加密插件设置', 'Lf_Password_Access' ), 
    '加密设置',  // 菜单名称
    'manage_options', 
    'Lf_Password_Access',  
    'Lf_Password_Access_setting_function',
    'dashicons-chart-pie' 
  );

    add_action( 'admin_init', 'register_Lf_Password_Access_settings' );
}


function register_Lf_Password_Access_settings() {

    register_setting( 'Lf_Password_Access_plugin_settings_group', 'Lf_Password_Access_title' );
    register_setting( 'Lf_Password_Access_plugin_settings_group', 'Lf_Password_Access_desc' );
    register_setting( 'Lf_Password_Access_plugin_settings_group', 'Lf_Password_Access_img' );
}

function Lf_Password_Access_setting_function() {
?>
<div class="wrap">
<h1>加密插件设置</h1>

<form method="post" action="options.php">
    <?php settings_fields( 'Lf_Password_Access_plugin_settings_group' ); ?>
    <?php do_settings_sections( 'Lf_Password_Access_plugin_settings_group' ); ?>
    <table class="form-table">
        <tr valign="top">
        <th scope="row">提示文字</th>
        <td><input type="text" name="Lf_Password_Access_title" style="margin-right:10px;width:500px;" value="<?php echo esc_attr( get_option('Lf_Password_Access_title') ); ?>" /></td>
        </tr>

        <tr valign="top">
        <th scope="row">说明文本</th>
        <td><textarea cols="25" rows="5" name="Lf_Password_Access_desc" style="margin-right:10px;width:500px;"><?php echo esc_attr( get_option('Lf_Password_Access_desc') ); ?></textarea></td>
        </tr>
        <tr valign="top">
        <th scope="row">二维码链接</th>
        <td><input type="text" name="Lf_Password_Access_img" style="margin-right:10px;width:500px;" id="Lf_Password_Access_img" value="<?php echo esc_attr( get_option('Lf_Password_Access_img') ); ?>" /><input data-id="Lf_Password_Access_img" type="submit" value="上传" class="upload_button"></td>
        </tr>
    </table>
    <?php submit_button(); ?>
</form>
</div>

找了很久,就以上这段代码比较简单!其他的都很复杂,且也不容易达成效果!

调用媒体库:

<!--上传图片-->		
<?php wp_enqueue_media();?>
<script>   
jQuery(document).ready(function(){   
	var upload_frame;   
	var value_id;   
	jQuery('.upload_button').on('click',function(e){   
		value_id =jQuery( this ).attr('data-id');
		event.preventDefault();   
		if( upload_frame ){   
			upload_frame.open();   
			return;   
		}   
		upload_frame = wp.media({   
			title: '插入图片',   
			button: {   
				text: '插入',   
			},   
			multiple: false   
		});   
		upload_frame.on('select',function(){  //里面是选择图片后的动作,把图片地址赋值给input 
			attachment = upload_frame.state().get('selection').first().toJSON();   
			jQuery('input[id='+value_id+']').val(attachment.url);   
		});	   
		upload_frame.open();   
	});   
});   
</script>

效果:

标签:

wordpress禁用REST API导致古腾堡编辑器发布文章出错问题

wordpress禁用REST API,在function.php中加入以下代码: add_filter("json_enabled", "__return_false"); add_filter("json_jsonp_enabled", "__return_false"); add_filter("rest_enabled", "__return_false"); add_filter("rest_jsonp_enabled", "__return_false"); remove_action("init", "rest_api_init"); remove_action("rest_ap
笔记 1,193

wordpress 后台主题设置选项按钮(button、input submit)点击屏蔽提交事件禁止刷新

wordpress 后台主题设置选项按钮(button、input submit)点击屏蔽提交事件禁止刷新 如果在设计主题或者插件后台设置选项时,wordpress后台的按钮(button、input submit)默认是提交操作,如只是响应JS事件,需要屏蔽提交,防止页面刷新。e.preventDefault(); 完整示例: $("button.copy").on("click",function(e){ e.preventDefault(); JS操作 });
笔记 1,071

wordpress 过滤垃圾评论有效方法

wordpress不管站是什么样的,只要开启了评论,就会有垃圾评论来光顾。这些垃圾评论,都有一个共同点,那就是全英文。 既然是全英文,对于国人,那就暴力一点,直接过滤点不含中文的评论。 在主题functions.php中添加: function my_comment_spam_filter($comment_id) { $comment = get_comment($comment_id); if (!preg_match('/[\x{4e00}-\x{9fa5}]/u',$comment->comment_content)) { wp_delete_com
笔记 2,319

wordpress评论模块,好久没写过了,大概是忘记了

不知道什么时候开始,或许是因为备案要求不能有交互式内容吧,自用模板都没有写评论模块 或许还有一个原因,就是垃圾评论太多了 很多网站都设置了登录才能评论,也别说评论了,现在估计也很少写文字了​。 今天要写一个留言板,需要评论模块,感觉都忘记了,查了下wordpress的评论模块函数 comment_form()​:评论表单 wp_list_comments()​:帖子列表 刚开始放上去,没有输出,依稀记得,wordpress是有内置表单的,不可能不会有输出的问题​。 这个评论是放在页面上的,理论上和文章模块都是一样的。原本以为是不支持页面,放文章页,也是不显示,​到后台看了下,原来是关闭了评论。
随笔 2,672

wordpress 分类页获取分类名称及该分类信息并显示文章数量

有一个这样的需求,要在分类页显示该分类下的所有文章数量,网上的写法大致都是用循环去叠加该分类下子分类的文章数量。 但是,其实最简单的写法是自己调用内置函数获取文章数量,包括在首页显示全站的所有文章数量。 $wp_query->found_posts 为了验证这个函数,我特意建立了一个空白的主题,在每个页面上都打印出 $wp_query 这个函数。 wordpresss主题主要的几个文件: header:页头 footer:页脚 index:主页 category:分类页 single:文章模板 page:页面模板 search:搜索模板 tag:标签模板 functions:函数文件 head
笔记 1,821