当前位置: 首页 » 教程 » wordpress插件开发 添加设置选项

wordpress插件开发 添加设置选项

零分教程2,3412023-05-24 20:08

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

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

很多以前的博客网站也都不更新了,有的资源也基本都是收费,且不说费用多少,在获得资源之前,也不知道是不是自己需要的资源,对于很多代码,接口,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>

效果:

END
零分站龄17年资深站长
一个喜欢折腾,却又折腾不出像样东西的,不会PHP的PHP程序员!
2517
文章
13
分类
3818
标签
3
友链
onlinelovesky317355746vipsever@vip.qq.com