WordPress provides the ability to contribute and login to contribute

Source: Internet
Author: User
Tags strlen trim

WordPress implements the contribute function

I. Add a contribution form

1. First create a php file under the Directory of the current topic, name it tougao-page.php, and then copy all the code in page. php to the tougao-page.php;

2. Delete all comments starting with the tougao-page.php, I .e./* and */, and all contents between them;

3. Set <? Php the_content ();?> Use the following code:

The code is as follows: Copy code

<? Php the_content ();?>

<Form method = "post" action = "<? Php echo $ _ SERVER ["REQUEST_URI"];?> ">
<Div style = "text-align: left; padding-top: 10px;">
<Label> nickname: * </label>
</Div>
<Div>
<Input type = "text" size = "40" value = "" name = "tougao_authorname"/>
</Div>

<Div style = "text-align: left; padding-top: 10px;">
<Label> Email: * </label>
</Div>
<Div>
<Input type = "text" size = "40" value = "" name = "tougao_authoremail"/>
</Div>

<Div style = "text-align: left; padding-top: 10px;">
<Label> Your Blog: </label>
</Div>
<Div>
<Input type = "text" size = "40" value = "" name = "tougao_authorblog"/>
</Div>

<Div style = "text-align: left; padding-top: 10px;">
<Label> Article Title: * </label>
</Div>
<Div>
<Input type = "text" size = "40" value = "" name = "tougao_title"/>
</Div>

<Div style = "text-align: left; padding-top: 10px;">
<Label> Category: * </label>
</Div>
<Div style = "text-align: left;">
<? Php wp_dropdown_categories ('show _ count = 1 & hierarchical = 1');?>
</Div>

<Div style = "text-align: left; padding-top: 10px;">
<Label> article content: * </label>
</Div>
<Div>
<Textarea rows = "15" cols = "55" name = "tougao_content"> </textarea>
</Div>

<Br clear = "all">
<Div style = "text-align: center; padding-top: 10px;">
<Input type = "hidden" value = "send" name = "tougao_form"/>
<Input type = "submit" value = "submit"/>
<Input type = "reset" value = "Refill"/>
</Div>
</Form>

2. Add Form processing code

In the tougao-page.php, place the first <? Php changed:

The code is as follows: Copy code

<? Php/** Template Name: tougao * @ author: Ludou * @ Email: zhouzb889@gmail.com * @ Blog: http://www.ludou.org /*/
If (isset ($ _ POST ['tougao _ form']) & $ _ POST ['tougao _ form'] = 'send ')
{
If (isset ($ _ COOKIE ["tougao"]) & (time ()-$ _ COOKIE ["tougao"]) <120)
{
Wp_die! ');
}

// Form variable initialization $ 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 )):'';

// Form item data verification if (empty ($ name) | strlen ($ name)> 20)
{
Wp_die ('Nickname is required and the length cannot exceed 20 words ');
}

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 is required, and the length cannot exceed 60 characters. It must comply with the Email format ');
}

If (empty ($ title) | strlen ($ title)> 100)
{
Wp_die ('title is required and the length cannot exceed 100 words ');
}

If (empty ($ content) | strlen ($ content)> 3000 | strlen ($ content) <100)
{
Wp_die ('the content must be entered, and the length must not exceed 3000 characters, not less than 100 words ');
}

$ Post_content = 'Nickname :'. $ name. '<br/> Email :'. $ email. '<br/> blog :'. $ blog. '<br/> Content :'. $ content;

$ Tougao = array (
'Post _ title' => $ title,
'Post _ content' => $ post_content,
'Post _ category '=> array ($ category)
);

// Insert the article into the database $ status = wp_insert_post ($ tougao );

If ($ status! = 0)
{
Setcookie ("tougao", time (), time () + 180 );
Wp_die ('contribution successful! Thank you for your contribution! ');
}
Else {
Wp_die ('submission failed! ');
}
}

Code Supplement: If you want to publish the Contributed article immediately without review and edit it, replace the 45 lines above:

The code is as follows: Copy code
'Post _ content' => $ post_content, 'post _ status' => 'Publish ',

Finally, go to the WordPress Management backend-page-create page, with the title as contribution (you can name it yourself), and enter the submission instructions. You can select a template on the right and select tougao, the basic contribution function has been added. For issues such as poor form styles and lack of projects you want, add css and form items by yourself.

How can I contribute after a member logs in? Please move:WordPress implements membership login and contribution

The following is a part of the code to be contributed after login. I believe that you can see this code to implement the functions you want.

The code is as follows: Copy code
If (! Is_user_logged_in ()){
?>
<H3 class = "base-tit"> You need to log in to submit your contribution! </H3>
<Form method = "post" action = "<? Php bloginfo ('URL');?> /Wp-login.php "name =" loginform "class =" submit-signin ">
<Ul>
<Li> <label> <B> user name: </B> <input type = "text" size = "20" value = "" name = "log" class = "ipt"> </label> </li>
<Li> <label> <B> password: </B> <input type = "password" size = "20" value = "" name = "pwd" class = "ipt"> </label> </li>
<Li> <input type = "submit" value = "log on now" name = "submit" class = "btn-primary"> </li>
<Li> <a href = "<? Php bloginfo ('URL');?> /Wp-login.php? Action = register "class =" btn-mini "> register </a> <a href =" <? Php bloginfo ('URL');?> /Wp-login.php? Action = lostpassword "class =" btn-mini "> retrieve password </a> </li>
</Ul>
<Input type = "hidden" value = "<? Php bloginfo ('URL');?> /Submit "name =" redirect_to ">
</Form>
<? Php} else {?>
<H3 class = "base-tit"> submit the article! </H3>
Here is the submission form. You can design it yourself ........
<? Php }?>

If you want the same effect as the big front-end, add the following HTML code to the form:

The code is as follows: Copy code
<H3 class = "base-tit"> You need to log in to submit your contribution! </H3>
<Form method = "post" action = "<? Php bloginfo ('URL');?> /Wp-login.php "name =" loginform "class =" submit-signin ">
<Ul>
<Li> <label> <B> user name: </B> <input type = "text" size = "20" value = "" name = "log" class = "ipt"> </label> </li>
<Li> <label> <B> password: </B> <input type = "password" size = "20" value = "" name = "pwd" class = "ipt"> </label> </li>
<Li> <input type = "submit" value = "log on now" name = "submit" class = "btn-primary"> </li>
<Li> <a href = "<? Php bloginfo ('URL');?> /Wp-login.php? Action = register "class =" btn-mini "> register </a> <a href =" <? Php bloginfo ('URL');?> /Wp-login.php? Action = lostpassword "class =" btn-mini "> retrieve password </a> </li>
</Ul>
<Input type = "hidden" value = "<? Php bloginfo ('URL');?> /Submit "name =" redirect_to ">
</Form>
<? Php} else {?>
<H3 class = "base-tit"> submit the article! </H3>
<Ul class = "submit-form">
<Li>
<Label>
<Span class = "submit-tit"> <em> * </em> Article title </span>
<Input type = "text" class = "EPT-submit-title u_ipt_error" placeholder = "write something..." size = "40">
<Span class = "u_tip" style = "display: inline-block;"> the title cannot be blank! </Span>
</Label>
</Li>
<Li>
<Label>
<Span class = "submit-tit"> <em> * </em> category </span>
<? Php wp_dropdown_categories ('show _ count = 1 & hierarchical = 1 & exclude = 1,97, 149,461 & class = ipt-submit-cat');?>
</Label>
</Li>
<Li>
<Label>
<Span class = "submit-tit"> <em> * </em> article URL </span>
<Input type = "url" class = "ipt ipt-submit-url" size = "40" value = "http: //">
<Span class = "u_tip"> </span>
</Label>
</Li>
<Li>
<Label>
<Span class = "submit-tit"> <em> * </em> Author </span>
<Input type = "url" class = "ipt ipt-submit-author" size = "40" value = "<? Php echo $ u_name;?> ">
<Span class = "u_tip"> </span>
</Label>
</Li>
<Li>
<Label>
<Span class = "submit-tit"> <em> * </em> email </span>
<Input type = "url" class = "ipt ipt-submit-email" size = "40" value = "<? Php echo $ u_mail;?> ">
<Span class = "u_tip"> </span>
</Label>
</Li>
<Li>
<Label>
<Span class = "submit-tit"> <em> * </em> content </span>
<Textarea placeholder = "write something..." class = "submit-content u_ipt_error"> </textarea>
<Span class = "u_tip u_tip_content" style = "display: inline-block;"> the content cannot be blank! </Span>
</Label>
</Li>
<Li>
<Input type = "button" value = "submit now" class = "btn-primary dosubmit">
<Input class = "btn" type = "reset" value = "Refill"/>
</Li>
<Li style = "padding-top: 10px;"> <span class = "tip"> </span> </li>
</Ul>

Then there is the CSS style:

The code is as follows: Copy code

<Style>
. Submit-form li {margin-bottom: 12px; padding-left: 100px; clear: both; position: relative ;}
. Submit-tit {width: 94px; float: left; margin-left:-100px; text-align: right; font-size: 14px; line-height: 26px ;}
. Submit-form label em {color: # f00; font-style: normal; font-family: fantasy; margin-right: 2px ;}
. Submit-form. ERT {width: 300px ;}
. Submit-form select {border: 1px solid # D2D2D2; width: 314px; padding: 2px; border-radius: 2px; height: 30px ;}
. Submit-form textarea {border: 1px solid # D2D2D2; width: 98%; padding: 5px; border-radius: 2px; height: 250px ;}
. Submit-form. btn-primary {width: 90px; margin-right: 10px ;}

. U_tip {position: absolute; margin-left: 10px; background-color: # F2DEDE; border: 1px solid # EED3D7; border-radius: 2px; height: 29px; line-height: 29px; padding: 0 10px; color: # B94A48; display: none ;}
. U_ipt_error,. submit-form textarea. u_ipt_error {border: 1px solid # E0BAD2; box-shadow: 0 0 4px # E0BAD2 ;}
. U_tip_content {bottom:-42px; left: 47%}
. Tip {background-color: # D9EDF7; border: 1px solid # BCE8F1; border-radius: 2px; padding: 6px 10px; color: # 3A87AD; font-weight: bold; display: none ;}

. Article-entry ul {margin-left: 36px ;}

. Submit-signin ul {padding: 10px 0 0 200px ;}
. Submit-signin li {clear: both; margin-bottom: 15px ;}
. Submit-signin B {float: left; width: 195px; text-align: right; margin-left:-200px; line-height: 29px; font-size: 14px ;}
. Submit-signin. EPT {width: 220px; font-weight: bold}
. Submit-signin. btn-primary {width: 100px ;}
. Submit-signin. btn-mini {margin-right: 10px ;}
</Style>

By the way, some parameters are required in the form. The following code calls the parameters:

The code is as follows: Copy code

<? Php
Global $ current_user;
Get_currentuserinfo ();
$ Cur_id = $ current_user-> ID;
$ User_info = get_userdata ($ cur_id );
$ U_login = $ user_info-> user_login;
$ U_mail = $ user_info-> user_email;
$ U_time = $ user_info-> user_registered;
$ U_name = get_user_meta ($ cur_id, 'nickname', true );
?>

Well, it should be around. I posted the code without reservation. Whether it can be implemented depends on your creation. As for how to submit, it is the core code of this site, which will not be shared here

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.