Wordpress submit post content from front-end with Ajax - ajax

I am generating my own Wordpress template and would like to give users a front-end form (so they don't have to log in to the wordpress dashboard) to submit some post content. I've got everything working, thanks to this tutorial - but I'd like to make one enhancement. Currently when the user presses submit the data is stored in wordpress and they are then redirected to a fixed URL of my choosing. Rather than this I would like to simply clear the form data and display a confirmation message - I guess via AJAX? I know there are built in AJAX capabilities in wordpress, but I've never really used it. Can anyone help me out?
The part of the code that submits the data to Wordpress and provides the URL to be redirected is copied below. I assume I wil need to make a change here?
$post_id = wp_insert_post($post_information);
if($post_id)
{
wp_redirect( '/thanks' );
exit;
}

You could simply redirect to the current page with get_permalink():
if ( $post_id )
{
wp_safe_redirect( add_query_arg( array( 'thanks' => '1' ), get_permalink() ) );
exit;
}
To display a message in the template, check the query arg:
if( isset( $_GET['thanks'] ) && '1' == $_GET['thanks'] )
echo '<p class="success">Thanks!</p>';

if you need more featuredone means, try this,
http://kvcodes.com/2014/02/front-end-post-submission-wordpress/

Related

do_shortcode not running ajaxlaodmore shortcode

I am using ajaxloadmore plugin. it works perfect from the code below inside template.
but when I call above code from the ajax call it returns empty divs with no records.
can someone let me know why it dosen't work from ajax call.
Ajax load more plugin uses ajax to load posts. Why are you not using its inbuilt functionality? You cannot use ajax inside PHP code for another ajax.
If you use this shortcode in ajax action, I don't think it will load it's JS files. If you could share some more details about what you are trying to achieve here, I might be able to help.
Also, if it does work somehow, you also need to manage the offset parameters or you will end up loading the same post again and again.
Edit 1-
You can reload the page when the user selects a category and by default, you can use all categories.
Try below code:-
if ( isset( $_GET[ 'category' ] ) && $_GET[ 'category' ] !== '' ) {
$cat = esc_html( $_GET[ 'category' ] );
} else {
$cat = "in-the-news,leadership,tips-tricks,trends";
}
echo do_shortcode( '[ajax_load_more post_type="post" posts_per_page="6" category="' . $cat . '" ]' );

client side validation not working for model window / ajax-loaded-form in yii

I am using Yii-user extension in the main layout i have a sign up link which is common to all the Cmenu
view/main layout
echo CHtml::link('Signup','#',array('id'=>'regi'));
$("#regi").click(function(){
$.ajax({
type:'GET',
url:'<?php echo Yii::app()->request->baseUrl;?>/index.php/user/registration',
success:function(res){
$("#dispdata").show();
$("#dispdata").html(res);
}
});
});
<div id="dispdata"><div>
**yii user extension **renders this perfectly and even submit its correctly if form values a re valid.
but if the values are incorrect and blank it redirect to url .../user/registration
which is not what my need .I need guidance what do i do such that if the values are incorrect or blank it should not redirect and display the errors in model window.
I did tried but hardly could get the satisfied results
if i place the following the model window itself doesnt appear what do i do
module registrationController i placed
....//some code here (**in yiiuser register controller**)
if ($model->save()) {
echo CJSON::encode(array(
'status'=>'success',
));
}
....//some code here...
Yii::app()->clientScript->scriptMap['jquery.js'] = false;
$this->renderPartial('registration',array('model'=>$model,),false,true);
in module view registration
<?php echo CHtml::ajaxSubmitButton(Yii::t('registration'),CHtml::normalizeUrl(array('user/registration','render'=>false)),array('dataType'=>'json',
'success'=>'function(data) {
if(data != null && data.status == "success") {
$("#registration-form").append(data.data);
}
}')); ?>
can anyone please guide me am working past 10 ten days tried every hook or crook method but could not obtain the results......how can the model window with client side validation be done appear..... Please guide me or let me know something better can be done
rules in registration model
if (!(isset($_POST['ajax']) && $_POST['ajax']==='registration-form')) {
array_push($rules,array('verifyCode', 'captcha', 'allowEmpty'=>!UserModule::doCaptcha('registration')));
as well was not with attributes for reqired field
have changed to
array_push($rules,array('verifyCode', 'captcha','message' => UserModule::t("captcha cannot be blank.")));
and added the verifycode to required field
yet not working,
The simple way is using render method in your Ajax action and creating empty layout for this action. If you do so, validation scripts will be included in the server response. Also you need to exclude jquery.js and other script with Yii::app()->clientScript->scriptMap and include them in main layout always.

ajaxbutton how to prevent refresh of the page

I succeed to use Ajax with Yii framework.
I renderPartial a form from within a list of post.
What I want to do is to prevent refresh when the user click on the ajaxbutton in the form.
In the beginning of the form I used the following code to activate ajax
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'post-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
)); ?>
and at the end of the page I simply have the ajaxbutton
<?php echo CHtml::ajaxSubmitButton('Save'); ?>
in the action controller I have the following
if(isset($_POST['ajax']) && $_POST['ajax']==='comment-form')
{
echo CActiveForm::validate($comment);
Yii::app()->end();
}
When I click on the ajax button, it saves the data but refresh the page, so it display the form.
What I want is to stay on the page.
Is anyone to help ?
Thank you in advance.
you can prevent the page from refresh, or ask the user if he's sure he want to leave the page by this code:
window.onbeforeunload = function() {
return "Dude, are you sure you want to leave? Think of the kittens!";
}
you can check this question: Prevent any form of page refresh using jQuery/Javascript
I think you have some thing like $this->redirect( ... ));
in your controller after $model->save() , right?
so don't redirect there

How to make sure a file is selected before uploading

Hi I am using codeigniter for file uploading and I am using this code
echo form_open_multipart('controller_a');
echo form_upload('userfile');
echo form_submit('Upload','upload');
echo form_close();
I store the pointer to the uploaded file in the database,
My question is how do I make sure that the user has selected a file before clicking on upload button because as of now the code submits even if the user clicks directly on upload without selecting a file
Along with client side verification, you should use server side verification, too. Currently, CodeIgniter does not provide a function, so one can use native PHP function is_uploaded_file:
if (is_uploaded_file($_FILES['myfile']['tmp_name']))
{
$this->load->library('upload');
$this->upload->do_upload('myfile');
}
You can't, not in CodeIgniter at least. You'll need to have JS overwrite the onsubmit property of the form and then test the userfile input's value.
use JS
very basic code, but it works.
<script type="text/javascript">
<!--
function validate_form ( )
{
valid = true;
if ( document.upload_form.something.value == "" )
{
alert ( "Please select a file before clicking upload ! " );
valid = false;
}
return valid;
}
//-->
</script>
and use onsubmit even in the form
onSubmit="return validate_form ( );"
when a user click on upload button without selecting any file, it will alert the user .
Your best bet is to use a jQuery plugin like the following:
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
This will allow you to select what input values will need to be selected, and customize a message to inform the user what field(s) they are missing.

cakephp updating elements

I have an index view which has some elements on it .
index controller code;
$userID = $this->Authsome->get('id');
$qnotes = $this->Qnote->getnotes($userID);
$this->set('qnotes', $qnotes)
$this->render();
elements have been added to the page using
index view code
<?php echo $this->element('lsidebar'); ?>
now the Issue is I also Have an add controller.
add controller code
function add() {
if(!empty($this->data)) {
unset($this->Qnote->Step->validate['qnote_id']);
$this->Qnote->saveAll($this->data);
$this->Session->setFlash('New Note Template has been added.','flash_normal');
}
}
now what I am trying to achieve is once I add a Qnote i want the element('lsidebar') updated
for the new Qnote.
I am Using the Ajax helper. found at http://www.cakephp.bee.pl/
also Here the add qnote View Code :
<?php echo $ajax->submit(
'Submit', array(
'url' => array(
'controller'=>'qnotes',
'action'=>'add')
));
I know its sound like a noob question . can Somebody point me in the right direction atleast.
I have tried everything i could think off. I bet the solution something easy which i didnt think off
help :)
If you want to dynamically update a sidebar with information that is submitted via ajax, there should be a "success" option in your ajax post that would allow you to fire a specific javascript action when the post is finished (or succeeds). You should write a small javascript ajax function to reload the contents of your sidebar when the post succeeds.
See this other stackoverflow answer: CakePHP ajax form submit before and complete will not work for displaying animated gif

Resources