WordPress 使用短代码实现部分内容输入密码才可以浏览,另外使用按需加载样式
前言
前几天群里无意中看到知更鸟主题对 faq 文章加密的方法,但是发现不太人道,所以准备用短代码,这个理论上应该可以的,百度下居然有现成的,自己稍微改改。主要代码来自E 享乐
代码
首先是建立短代码啦
function e_secret($atts, $content=null){ extract(shortcode_atts(array('key'=>null), $atts)); if(isset($_POST['e_secret_key']) && $_POST['e_secret_key']==$key){ return ' <div class="e-secret">'.$content.'</div> '; } else{ return ' <form class="e-secret" action="'.get_permalink().'" method="post" name="e-secret"><label>输入密码查看加密内容:</label><input type="password" name="e_secret_key" class="euc-y-i" maxlength="50"><input type="submit" class="euc-y-s" value="确定"> <div class="euc-clear"></div> </form> '; } } add_shortcode('ssecret','e_secret');
然后就是加载样式啊,这里是云落自己写的代码,我在这里设定了一下,只有上面那个短代码的页面才加载样式,其他页面一律不加载,这个是比较不错的地方的
//加载密码可见的样式 function secret_css() { global $post,$posts; foreach ($posts as $post) { if ( has_shortcode( $post->post_content, 'ssecret') ){ echo '<style type="text/css">.e-secret{margin:20px 0;padding:20px;height:60px;background:#f8f8f8}.e-secret input.euc-y-i[type=password]{float:left;background:#fff;width:100%;line-height:36px;margin-top:5px;border-radius:3px}.e-secret input.euc-y-s[type=submit]{float:right;margin-top:-47px;width:30%;margin-right:1px;border-radius:0 3px 3px 0}input.euc-y-s[type=submit]{background-color:#3498db;color:#fff;font-size:21px;box-shadow:none;-webkit-transition:.4s;-moz-transition:.4s;-o-transition:.4s;transition:.4s;-webkit-backface-visibility:hidden;position:relative;cursor:pointer;padding:13px 20px;text-align:center;border-radius:50px;-webkit-box-shadow:none;-moz-box-shadow:none;box-shadow:none;border:0;height:auto;outline:medium;line-height:20px;margin:0}input.euc-y-s[type=submit]:hover{background-color:#5dade2}input.euc-y-i[type=password],input.euc-y-i[type=text]{border:1px solid #F2EFEF;color:#777;display:block;background:#FCFCFC;font-size:18px;transition:all .5s ease 0;outline:0;box-sizing:border-box;-webkit-border-radius:25px;-moz-border-radius:25px;border-radius:25px;padding:5px 16px;margin:0;height:auto;line-height:30px}input.euc-y-i[type=password]:hover,input.euc-y-i[type=text]:hover{border:1px solid #56b4ef;box-shadow:0 0 4px #56b4ef}</style>';}}} add_action('wp_head', 'secret_css');
最后就是给编辑器添加个按钮啦
//添加编辑器密码可见按钮 function mmkj_tags($mce_settings) { ?> <script type="text/javascript"> QTags.addButton( 'mimakejian', '密码可见', '[ssecret key="输入密码"]', '[/ssecret]' ); function yunluo_shortcode() { } </script> <?php } add_action('after_wp_tiny_mce', 'mmkj_tags');
OK!大功告成!
预览
密码是:2233