I'm trying to get CakePHP's Security Component for CSFR Protection working with AJAX.
I have my ArtistsDates-Controller (to save all the dates of shows an Artist/DJ has), which contains an addedit() - view.
This view is loaded via jQuery AJAX into a jQuery Modalbox. (SimpleModal)
function artist_dates(request){
.
.
if(request == 'load'){
$.ajax({
type: 'post',
url: $('base').attr('href') + '/artist_dates/addedit/'+artist_id,
success: function(html){
$('#dialog').html(html);
$('#dialog').modal({
modal: false,
maxHeight:'500px',
minHeight:500,
minWidth:750,
});
}
});
}
.
.
}
In this View, my Form is rendered as addedit_daterow_form - Element. This element is either called with data or in "NEW"-Mode. If data is provided, the element displays the data and contains a hidden-edit form. If its called in "NEW"-Mode, it returns an empty Form. So, this element is rendered for every datarow in the ArtistDate - Model (+1 more for adding a new one!)
(here's a screenshot of the view: http://i.stack.imgur.com/Ye10v.png)
Security-Component is included in the ArtistDatesController. Unfortunately $this->Form->request->params neither contains the [_Token] in the addedit- view nor in the addedit_daterow_form- element - do I have to change something in my jQuery-AJAX-Function?
--
EDIT 1: This is how my Form-Code looks like:
<?php echo $this->Form->create('ArtistDate', array('controller' => 'artist_dates','action' => 'addedit', 'id' => 'artistDateForm_'.$date_nr)); ?>
<?php echo pr($this->Form->request->params); ?>
<?php echo $this->Form->input('ArtistDate.'.$date_nr.'.id',array('type' => 'hidden', 'value' => $date['ArtistDate']['id'])); ?>
<?php echo $this->Form->input('ArtistDate.'.$date_nr.'.artist_id',array('type' => 'hidden', 'value' => $date['ArtistDate']['artist_id'])); ?>
<div class="date">
<?php echo $this->Form->input('ArtistDate.'.$date_nr.'.date', array('type' => 'text','label' => 'Date <span style="font-weight:normal; float:right;">[DD.MM.YYYY]</span>','value' => (!empty($date['ArtistDate']['date']) ? date('d.m.Y',strtotime($date['ArtistDate']['date'])) : ''))); ?>
<?php echo $this->Form->input('ArtistDate.'.$date_nr.'.date_end', array('type' => 'text','label' => 'Enddate <span style="font-weight:normal; float:right;">[DD.MM.YYYY]</span>','value' =>(!empty($date['ArtistDate']['date_end']) ? date('d.m.Y',strtotime($date['ArtistDate']['date_end'])) : ''))); ?>
</div>
<div class="venue">
<?php echo $this->Form->input('ArtistDate.'.$date_nr.'.venue', array('type' => 'text','value' => $date['ArtistDate']['venue'])); ?>
<?php echo $this->Form->input('ArtistDate.'.$date_nr.'.city', array('type' => 'text','value' => $date['ArtistDate']['city'])); ?>
</div>
<div class="link">
<?php echo $this->Form->input('ArtistDate.'.$date_nr.'.venuelink', array('type' => 'text','label' => 'Link <span style="font-weight:normal; float:right;">Venue</span>','value' => $date['ArtistDate']['venuelink'])); ?>
<?php echo $this->Form->input('ArtistDate.'.$date_nr.'.ticketslink', array('type' => 'text','label' => 'Link <span style="font-weight:normal; float:right;">Tickets</span>','value' => $date['ArtistDate']['ticketslink'])); ?>
</div>
<div class="actions">
<?php echo $this->Html->link('','',array('class' => 'buttonsave','onclick' => "artistdate_handling('".$date_nr."','save'); return false;", 'style' => $display_exists, 'escape' => false, 'title' => 'Save')); ?>
<?php echo $this->Html->link('','',array('class' => $approveclass, 'onclick' => "artistdate_handling('".$date_nr."','confirm'); return false;", 'style' => $display_exists, 'escape' => false, 'title' => 'Confirm Show')); ?>
<?php echo $this->Html->link('','',array('class' => 'buttondelete','onclick' => "artistdate_handling('".$date_nr."','delete'); return false;", 'style' => $display_exists, 'escape' => false, 'title' => 'Delete Show')); ?>
<?php echo $this->Html->link('','',array('class' => 'buttonadd','onclick' => "artistdate_handling('".$date_nr."','add'); return false;", 'style' => $display_new, 'escape' => false, 'title' => 'Add Show')); ?>
</div>
<div style="clear:both"></div>
<?php echo $this->Form->end(); ?>
Thanks a lot in advance!
Figured out a way how it works.
Using
$.ajax({
type: 'get'
.
.
});
returns a form containing the token.
Related
I want to display all posts as list category wise of a custom post type.
for Ex.:
demo Cat 1
- Post of category demo 1
- Post of category demo 1
- Post of category demo 2
- Post of category name 2
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1, //show all posts
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $term->slug,
)
)
);
$posts = new WP_Query($args);
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>
<?php if(has_post_thumbnail()) { ?>
<?php the_post_thumbnail(); ?>
<?php }
/* no post image so show a default img */
else { ?>
<img src="<?php bloginfo('template_url'); ?>/assets/img/default-img.png" alt="<?php echo get_the_title(); ?>" title="<?php echo get_the_title(); ?>" width="110" height="110" />
<?php } ?>
<?php echo get_the_title(); ?>
<?php the_excerpt(); ?>
<?php endwhile; endif; ?>
https://wordimpress.com/loop-through-categories-and-display-posts-within/
here is my code, I am using wp-paginate plugin in wordpress. I have tried many thing like replacing twentyfourteen_paging_nav(); with
if(function_exists('wp_paginate')) {
wp_paginate();
}
else {
twentyfourteen_paging_nav();
}
in archieve.php but still its not working.
<ul class="blog-list">
<?php
$blog = array(
'category' => '8', // post_id of category
'order' => 'Asc',
'posts_per_page' => 4,
'paged' => $paged
);
$blog_post = get_posts( $blog );
foreach ($blog_post as $post) : setup_postdata( $post );
$blog_img = $post->ID;
$blog_title = $post->post_title;
$blog_para = $post->post_content;
$blog_trimed=wp_trim_words($blog_para, 45);
$blog_name = get_post_meta($post->ID, 'author name', true);
?>
<li>
<div class="blog-content-left">
<?php echo get_the_post_thumbnail($blog_img, array(420,250)); ?>
</div>
<div class="blog-content-right">
<?php echo $blog_title; ?>
<p class="blog-date-area"><span class="blog-date"><?php echo get_the_date('M d, Y'); ?></span><span class="blog-date blog-crcl"><?php echo $blog_name; ?></span></p>
<div class="blog-para">
<p><?php echo $blog_trimed; ?></p>
</div>
<span class="read-more-btn-blue">
Read More
</span>
</div>
</li>
<?php endforeach;?>
</ul>
<?php echo wp_paginate(); ?>
Thanks for help in advance.
I didn't find any solutions, so I used pagednavi.
I have looked all over Google, trying to figure this out. I've made some progress but still stuck. I'm pretty new to ACF and custom post types. I have a custom post type of Attorneys that I setup through WCK. That post type has a field group with field names of attorney_photo, attorney_name and attorney_areas_of_practice. With the code below, I can get the attorney_name and attorney_areas_of_practice (repeater field) to display, but not the attorney_photo. I have the info displaying correctly on each attorneys specific page, but I need this page to be a listing of all the attorneys. Not sure what I am doing wrong with the image part.
<?php get_header(); ?>
<?php
$args = array(
'posts_per_page' => 30,
'order' => 'ASC',
'orderby' => 'title',
'post_type' => 'attorneys'
);
query_posts($args);
if ( have_posts() ) :
?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="attorney-preview">
<?php $photo = get_post_meta($post->ID,'attorney_photo', true); ?>
<img src="<?php echo $photo; ?>" />
<p><strong><?php echo get_post_meta($post->ID,'attorney_name', true); ?></strong></p>
<ul>
<?php
while ( have_rows('attorney_areas_of_practice') ) : the_row();
$attorney_area_of_practice = get_sub_field('attorney_area_of_practice');
echo "<li>" . $attorney_area_of_practice . "</li>";
endwhile;
?>
</ul>
</div><!-- .attorney-preview -->
<?php endwhile; ?>
<?php endif; ?>
<?php
wp_reset_query(); // Restore global post data stomped by the_post().
?>
<?php get_footer(); ?>
When you add image field through ACF plugin there are some options of return value. For example return value is: image object or return value is: image url, in your case return value might be selected as image object, not image url. That is why here with your code is being returned an array rather than only url. To get only image url from this array, please write as following a bit change:
<?php $photo = get_post_meta($post->ID,'attorney_photo', true); ?>
<img src="<?php echo $photo['url']; ?>" />
Try this
<?php query_posts(array('posts_per_page' => 30,'order' => 'ASC','orderby' => 'title','post_type' => 'attorneys'));
if (have_posts()) : while (have_posts()) : the_post();
$attorney_photo = get_post_meta($post->ID, 'attorney_photo', true);
$attorney_name = get_post_meta($post->ID, 'attorney_name', true); ?>
<div class="attorney-preview">
<img src="<?php echo $attorney_photo; ?>" />
<p><strong><?php echo $attorney_name; ?></strong></p>
<ul>
<?php
while ( have_rows('attorney_areas_of_practice') ) : the_row();
$attorney_area_of_practice = get_sub_field('attorney_area_of_practice');
echo "<li>" . $attorney_area_of_practice . "</li>";
endwhile;
?>
</ul>
</div><!-- .attorney-preview -->
<?php endwhile; endif; ?>
<?php
wp_reset_query(); // Restore global post data stomped by the_post().
?>
First data loading is fine but when i click on load more my second page data going inside even div only.
I am generating list like this:
<div class="odd">
<items>item1</items>
<items>item3</items>
<items>item5</items>
<items>item7</items>
</div>
<div class="even">
<items>item2</items>
<items>item4</items>
<items>item6</items>
<items>item8</items>
</div>
With this custom ListView class:
class ListViewOdd extends ListView
{
public function renderItems()
{
$models = $this->dataProvider->getModels();
$keys = $this->dataProvider->getKeys();
$rowsOdd = $rowsEven = [];
foreach (array_values($models) as $index => $model) {
if ($index%2 == 0) {
$rowsOdd[] = $this->renderItem($model, $keys[$index], $index);
} else {
$rowsEven[] = $this->renderItem($model, $keys[$index], $index);
}
}
return '<div class="odd">'.implode($this->separator, $rowsOdd) . '</div><div class="even">'.implode($this->separator, $rowsOdd) .'</div>'; // replace <div> to Html::tag('div', ...)
}
}
echo ListViewOdd::widget([
'dataProvider' => $dataProvider,
'itemView' => '_post',
]);
But load more pagination not splitting data again into odd/even listing as my first data list.
i am not passing anything from controller and action i am using model to get data provider
<?php echo ListViewOdd::widget([
'dataProvider' => Posts::getCommonListData($industry,'user','engage',0),
'itemOptions' => ['class' => 'item post-item'],
'summary' => '',
'id' => 'my-listview-id',
'itemView' => '_Posts',
'viewParams' => [
'fullView' => true,
],
'pager' => [
'class' => \app\vendor\kop\y2sp\ScrollPager::className(),
//'negativeMargin' => '200',
'triggerText' => 'Load More',
//'triggerOffset' => 3,
'noneLeftText' => '',
],
]);
getting output like this
<div class="odd">
<items>item1</items>
<items>item3</items>
<items>item5</items>
<items>item7</items>
</div>
<div class="even">
<items>item2</items>
<items>item4</items>
<items>item6</items>
<items>item8</items>
<items>item9</items>
<items>item10</items>
<items>item11</items>
<items>item12</items>
</div>
after clicking loadmore its just loading all records inside even div and load more aoption also coming under even div
Change in method renderItems:
return '<div class="odd">'.implode($this->separator, $rowsOdd) . '</div><div class="even">'.implode($this->separator, $rowsEven) .'</div>';
Mistake is ''.implode($this->separator, $rowsOdd) .''.
Div is even, but data from $rowsOdd :)
Edited: Answered below.
I'm using the following query to paginate my posts on a Custom Page template that pulls a Custom Post type. this exact code works on Author.php, but this custom template Page, "Filmmaking" does not show the pagination. I have a simple, tried and true pagination function after my loop. Why would it work for author.php, but not for page-filmmaking.php?
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$film_query = array(
'post_type' => 'filmmaking', // Custom Post Type
'paged' => $paged, // Set it up to be paged so you can use pagination
'posts_per_page' => 2, // How many to show per page
);
// The Query
$the_query = new WP_Query( $film_query );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
Answer: Get rid of if has_posts.
Working Code:
<?php
/**
* Template Name: pagination test
*/
get_header(); ?>
<?php //get_sidebar(); ?>
<div id="page-left">
<?php
$wp_query = new WP_Query();
$wp_query->query(array(
'post_type'=>'filmmaking',
'paged' => $paged,
'posts_per_page' => 2,
));
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php previous_posts_link('« Nyare') ?>
<?php next_posts_link(' Äldre »') ?>
</div><!-- page left -->
<?php get_footer(); ?>