中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

wordpress前端用戶投稿功能制作

2018-11-02    來源:學(xué)做網(wǎng)站論壇

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用

很多建網(wǎng)站學(xué)員學(xué)習(xí)了WordPress教程后,使用Wordpress 程序建了網(wǎng)站。但如果每天都是自己更新文章,感覺會(huì)有點(diǎn)累。為了減輕發(fā)文章的勞動(dòng)量,可以給自己的網(wǎng)站添加wordpress前端用戶投稿功能。

wordpress前端用戶投稿功能

如何制作wordpress前端用戶投稿功能呢?下面是學(xué)做網(wǎng)站論壇分享的方法。

方法/步驟

  1. 在主題文件夾下面新建一個(gè)【tougao.php】文件,把主題文件page.php里面的代碼全選復(fù)制到文件中;(如果對Wordpress模板文件不了解,可以學(xué)習(xí)一下wordpress模板制作教程)
  2. 打開tougao.php,在第一個(gè)【< ?php】代碼(一般是??) 后面敲幾個(gè)回車,然后把以下代碼粘貼進(jìn)來。
    
    
    /*
    Template Name: 投稿模板
    */
    if( isset($_POST['tougao_form']) && $_POST['tougao_form'] == ‘send’)
    {
    global $wpdb;
    $last_post = $wpdb->get_var(“SELECT post_date FROM $wpdb->posts WHERE post_type = ‘post’ ORDER BY post_date DESC LIMIT 1″);
    // 博客當(dāng)前最新文章發(fā)布時(shí)間與要投稿的文章至少間隔120秒。
    // 可自行修改時(shí)間間隔,修改下面代碼中的120即可
    // 相比Cookie來驗(yàn)證兩次投稿的時(shí)間差,讀數(shù)據(jù)庫的方式更加安全
    if ( current_time(‘timestamp’) – strtotime($last_post) < 120 )
    {
    wp_die(‘您投稿也太勤快了吧,先歇會(huì)兒!’);
    }

    // 表單變量初始化
    $name = isset( $_POST['tougao_authorname'] ) ? trim(htmlspecialchars($_POST['tougao_authorname'], ENT_QUOTES)) : ”;
    $email = isset( $_POST['tougao_authoremail'] ) ? trim(htmlspecialchars($_POST['tougao_authoremail'], ENT_QUOTES)) : ”;
    $blog = isset( $_POST['tougao_authorblog'] ) ? trim(htmlspecialchars($_POST['tougao_authorblog'], ENT_QUOTES)) : ”;
    $title = isset( $_POST['tougao_title'] ) ? trim(htmlspecialchars($_POST['tougao_title'], ENT_QUOTES)) : ”;
    $category = isset( $_POST['cat'] ) ? (int)$_POST['cat'] : 0;
    $content = isset( $_POST['tougao_content'] ) ? trim(htmlspecialchars($_POST['tougao_content'], ENT_QUOTES)) : ”;

    // 表單項(xiàng)數(shù)據(jù)驗(yàn)證
    if ( empty($name) || mb_strlen($name) > 20 )
    {
    wp_die(‘昵稱必須填寫,且長度不得超過20字’);
    }

    if ( empty($email) || strlen($email) > 60 || !preg_match(“/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix”, $email))
    {
    wp_die(‘Email必須填寫,且長度不得超過60字,必須符合Email格式’);
    }

    if ( empty($title) || mb_strlen($title) > 100 )
    {
    wp_die(‘標(biāo)題必須填寫,且長度不得超過100字’);
    }

    if ( empty($content) || mb_strlen($content) > 3000 || mb_strlen($content) < 100)
    {
    wp_die(‘內(nèi)容必須填寫,且長度不得超過3000字,不得少于100字’);
    }

    $post_content = ‘昵稱: ‘.$name.’<br />Email: ‘.$email.’<br />blog: ‘.$blog.’<br />內(nèi)容:<br />’.$content;

    $tougao = array(
    ‘post_title’ => $title,
    ‘post_content’ => $post_content,
    ‘post_category’ => array($category)
    );
    // 將文章插入數(shù)據(jù)庫
    $status = wp_insert_post( $tougao );

    if ($status != 0)
    {
    // 投稿成功給博主發(fā)送郵件
    // somebody#example.com替換博主郵箱
    // My subject替換為郵件標(biāo)題,content替換為郵件內(nèi)容
    wp_mail(“somebody#example.com”,”My subject”,”content”);
    wp_die(‘投稿成功!感謝投稿!’, ‘投稿成功’);
    }
    else
    {
    wp_die(‘投稿失!’);
    }
    }
  3. 找到page.php里面的文章內(nèi)容調(diào)用代碼,一般是【< ?php the_content(); ?>】,在后面粘貼以下表單代碼。
    
    
    <form id=”tougaoform” method=”post” action=”<?php echo $_SERVER["REQUEST_URI"]; ?>”>
    <p><input id=”author” type=”text” size=”40″ value=”" name=”tougao_authorname” /><label>昵稱(*必填)</label></p>
    <p><input id=”email” type=”text” size=”40″ value=”" name=”tougao_authoremail” /><label>郵箱(*必填)</label></p>
    <p><input id=”url” type=”text” size=”40″ value=”" name=”tougao_authorblog” /><label>您的博客/文章來源</label></p>
    <p><input id=”tougao_title” type=”text” size=”40″ value=”" name=”tougao_title” /><label>文章標(biāo)題(*必填)</label></p>
    <p><input id=”tags” type=”text” size=”40″ value=”" name=”tougao_tags” /><label>文章標(biāo)簽(多個(gè)標(biāo)簽請用英文逗號 , 分開)</label></p>
    <p><?php wp_dropdown_categories(‘show_option_none=請選擇文章分類&show_count=1& hierarchical=1&hide_empty=0′); ?><label>文章分類(*必填)</label></p>
    <textarea rows=”15″ cols=”55″ id=”tougao” name=”tougao_content”></textarea>
    <p>
    <input type=”hidden” value=”send” name=”tougao_form” />
    <input id=”submit” type=”submit” value=”提交” />
    <input id=”reset” type=”reset” value=”重填” />
    </p>
    </form>
  4. 在網(wǎng)站后臺新建一個(gè)page頁面,可以隨便寫一個(gè)標(biāo)題,比如“歡迎投稿”,然后在內(nèi)容里面寫上你的投稿要求。
  5. 在頁面發(fā)布下面有一個(gè)頁面屬性,在【模板】里面選擇【投稿模板】;
  6. 這樣,wordpress前端用戶投稿功能就制作好了。

標(biāo)簽: 安全 代碼 建網(wǎng)站 數(shù)據(jù)庫

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:wordpress找回密碼的3種方法

下一篇:織夢網(wǎng)站搬家后圖片不顯示解決方法