Paragraph breaks missing from shortcode output - shortcode

I created a shortcode in Wordpress to perform a query and display the content, but the content line breaks are being removed.
add_shortcode( 'resource' , 'Resource' );
function Resource($atts) {
$atts = shortcode_atts( array(
'category' => ''
), $atts );
$categories = explode(',' , $atts['category']);
$args = array(
'post_type' => 'resource',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page'=> -1,
'tax_query' => array( array(
'taxonomy' => 'category',
'field' => 'term_id',
'operator' => 'AND',
'terms' => $categories
) )
);
$string = '';
$query = new WP_Query( $args );
if( ! $query->have_posts() ) {
$string .= '<p>no listings at this time...</p>';
}
while( $query->have_posts() ){
$query->the_post();
$string .= '<div id="links"><div id="linksImage">' . get_the_post_thumbnail() . '</div>
<div id="linksDetails"><h1>'. get_the_title() .'</h1><p>' . get_the_content() . '</p>
<p>for more information CLICK HERE</div></div>';
}
wp_reset_postdata();
$output = '<div id="linksWrapper">' . $string . '</div>';
return $output;
}
Any suggestion on why this is happening and what to do to fix it. This is only happening on the shortcode output. On regular pages - the content displays correctly.

found a solution through more searches:
function get_the_content_with_formatting ($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
$content = get_the_content($more_link_text, $stripteaser, $more_file);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
return $content;
}
works perfect, so I thought I would share..

Related

Custom FacetWP dropdown not showing posts

I have been able to render a dropdown facet in FacetWP front end with:
add_filter( 'facetwp_facet_html', function( $output, $params ) {
if ( 'today_or_month' == $params['facet']['name'] ) {
$selected = isset( $params['selected'] ) ? $params['selected'] : '';
$output = '<select class="facetwp-dropdown">';
$output .= '<option value="">Select date range</option>';
$output .= '<option value="today"' . ( 'today' == $selected ? ' selected' : '' ) . '>Today</option>';
$output .= '<option value="month"' . ( 'month' == $selected ? ' selected' : '' ) . '>This Month</option>';
$output .= '<option value="test"' . ( 'test' == $selected ? ' selected' : '' ) . '>Test</option>';
$output .= '</select>';
}
return $output;
}, 10, 2 );
All 'event' posts show under the Facet dropdown on page load but when I select an option from the dropdown the queries do not return any posts in the FacetWP template. When I select "Select date range" again all the posts show again.
The queries are as follows:
add_filter( 'facetwp_query_args', function( $args, $class ) {
if (!isset($class->http_params['get']['facetwp'])) return $args;
$selected = $class->http_params['get']['facetwp']['today_or_month'];
$current_month = date( 'Y-m' );
if ( 'today' == $selected ) {
$args['meta_query'][] = array(
'key' => 'event_date_start',
'value' => date( 'Y-m-d' ),
'compare' => '<=',
);
$args['meta_query'][] = array(
'key' => 'event_date_end',
'value' => date( 'Y-m-d' ),
'compare' => '>=',
);
}
elseif ( 'month' == $selected ) {
$args['meta_query'][] = array(
'key' => 'event_date_start',
'value' => date( 'Y-m-01' ),
'compare' => '>=',
);
$args['meta_query'][] = array(
'key' => 'event_date_end',
'value' => date( 'Y-m-t' ),
'compare' => '<=',
);
}
elseif ( 'test' == $selected ) {
$args['meta_query'][] = array(
'key' => 'event_date_start',
'value' => $current_month,
'compare' => 'LIKE',
);
}
$args['post_type'] = 'event';
return $args;
}, 10, 2 );
Does anybody know why the query is not returning any posts in the front end?
I thought maybe I need to convert to a timestamp from ACF custom fields first? Something like this:
$start_date = get_field( 'event_date_start', $params['post_id'] );
$end_date = get_field( 'event_date_end', $params['post_id'] );
$start_timestamp = strtotime( $start_date );
$end_timestamp = strtotime( $end_date );
And then use these variables as the key:
'key' => '$start_timestamp ',
Any help at all would be great. Been going over and over this! Thank you so much. I am begging.
I have tried to run the code as above, I expected some of the custom post types of event to show up in the FacetWP template area according to the dropdown selections. Instead no posts show at all (they should show as some of the posts have February dates).

Send email to all the rows from mysql in codeigniter

i need to send emails to all the rows in database table with their corresponsing data. i am able to send single emails by their id, but now i need to send in bulk. i have done in pure php but in codeigniter not able to figure out. how to do this.
my controller is
public function autoformcomplete()
{
$this->load->model('admin/Reminder_model');
$datan = $this->Reminder_model->autoformemail();
//this should be in whileloop, that i am not able to figure out
$email = $datan[0]['email'];
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'email-smtp.eu-west-1.amazonaws.com',
'smtp_port' => 587,
'smtp_crypto' => 'tls',
'smtp_user' => 'user',
'smtp_pass' => 'pass',
'mailtype' => 'text',
'charset' => 'utf-8',
);
$this->email->initialize($config);
$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");
$this->email->set_crlf("\r\n");
$subject = "Complete Your Partially Filled Application-".$appid."";
$data['datan'] = $datan;
$mesg = $this->load->view('email/reminder/form_complete',$data,true);
$this->email->to($email);
$this->email->from('mail#mail.com','My Company');
$this->email->subject($subject);
$this->email->message($mesg);
$this->email->send();
echo "sent";
}
My Model is
public function autoformemail(){
$this->db->order_by('id', 'DESC');
$query = $this->db->get('appstbldata');
return $query->result_array();
}
Just use a foreach loop
public function autoformcomplete() {
$this->load->model( 'admin/Reminder_model' );
$datan = $this->Reminder_model->autoformemail();
foreach ( $datan as $index => $val ) {
// $val now references the current pointer to an element in your $datan array
$email = $val['email'];
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'email-smtp.eu-west-1.amazonaws.com',
'smtp_port' => 587,
'smtp_crypto' => 'tls',
'smtp_user' => 'user',
'smtp_pass' => 'pass',
'mailtype' => 'text',
'charset' => 'utf-8',
);
$this->email->initialize( $config );
$this->email->set_mailtype( "html" );
$this->email->set_newline( "\r\n" );
$this->email->set_crlf( "\r\n" );
$subject = "Complete Your Partially Filled Application-" . $appid . "";
$data['datan'] = $datan;
$mesg = $this->load->view( 'email/reminder/form_complete', $data, true );
$this->email->to( $email );
$this->email->from( 'mail#mail.com', 'My Company' );
$this->email->subject( $subject );
$this->email->message( $mesg );
$this->email->send();
echo "sent";
}
}
I dunno what your point of $data['datan'] = $datan; line is though

register View-Helper understanding issue

I tried to register a View Helper for navigation, it is an example from olegkrivtsov,I chose this to learn more about the topic. I also read the posts about it. I thought it must be really easy, but it doesn't work, probably some more experienced Zend-developer will see the problem immediately.
First the folder I use, is this the right folder, what is the diffenrence to the folder helpers in the module Import for example?
Here is the content of menu.php
<?php
namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper;
// This view helper class displays a menu bar.
class Menu extends AbstractHelper
{
// Menu items array.
protected $items = [];
// Active item's ID.
protected $activeItemId = '';
// Constructor.
public function __construct($items=[])
{
$this->items = $items;
}
// Sets menu items.
public function setItems($items)
{
$this->items = $items;
}
// Sets ID of the active items.
public function setActiveItemId($activeItemId)
{
$this->activeItemId = $activeItemId;
}
// Renders the menu.
public function render()
{
if (count($this->items)==0)
return ''; // Do nothing if there are no items.
$result = '<nav class="navbar navbar-default" role="navigation">';
$result .= '<div class="navbar-header">';
$result .= '<button type="button" class="navbar-toggle" ';
$result .= 'data-toggle="collapse" data-target=".navbar-ex1-collapse">';
$result .= '<span class="sr-only">Toggle navigation</span>';
$result .= '<span class="icon-bar"></span>';
$result .= '<span class="icon-bar"></span>';
$result .= '<span class="icon-bar"></span>';
$result .= '</button>';
$result .= '</div>';
$result .= '<div class="collapse navbar-collapse navbar-ex1-collapse">';
$result .= '<ul class="nav navbar-nav">';
// Render items
foreach ($this->items as $item) {
$result .= $this->renderItem($item);
}
$result .= '</ul>';
$result .= '</div>';
$result .= '</nav>';
return $result;
}
// Renders an item.
protected function renderItem($item)
{
$id = isset($item['id']) ? $item['id'] : '';
$isActive = ($id==$this->activeItemId);
$label = isset($item['label']) ? $item['label'] : '';
$result = '';
if(isset($item['dropdown'])) {
$dropdownItems = $item['dropdown'];
$result .= '<li class="dropdown ' . ($isActive?'active':'') . '">';
$result .= '<a href="#" class="dropdown-toggle" data-toggle="dropdown">';
$result .= $label . ' <b class="caret"></b>';
$result .= '</a>';
$result .= '<ul class="dropdown-menu">';
foreach ($dropdownItems as $item) {
$link = isset($item['link']) ? $item['link'] : '#';
$label = isset($item['label']) ? $item['label'] : '';
$result .= '<li>';
$result .= ''.$label.'';
$result .= '</li>';
}
$result .= '</ul>';
$result .= '</a>';
$result .= '</li>';
} else {
$link = isset($item['link']) ? $item['link'] : '#';
$result .= $isActive?'<li class="active">':'<li>';
$result .= ''.$label.'';
$result .= '</li>';
}
return $result;
}
}
I posted the hole example for somebody who also wants to use it.
Here how I tried to register in my module.config.php
'view_helpers' => [
'factories' => [
View\Helper\Menu::class => InvokableFactory::class,
],
'aliases' => [
'mainMenu' => View\Helper\Menu::class
]
],
I placed it in the layout.phtml
<div class="collapse navbar-collapse">
<?php
$this->mainMenu()->setItems([
[
'id' => 'home',
'label' => 'Dashboard',
'link' => $this->url('home')
],
[
'id' => 'project',
'label' => 'Project',
'link' => $this->url("project", ['action'=>'index'])
],
[
'id' => 'unit',
'label' => 'Unit',
'dropdown' => [
[
'id' => 'add',
'label' => 'add Unit',
// 'link' => $this->url('unit', ['page'=>'contents'])
'link' => $this->url('unit', ['action'=>'add'])
],
[
'id' => 'help',
'label' => 'Help',
'link' => $this->url('home')
]
]
],
]);
echo $this->mainMenu()->render();
?>
</div>
With this code I replaced the former part, which came from the skeleton:
<div class="collapse navbar-collapse">
<?= $this->navigation('navigation')
->menu()
->setMinDepth(0)
->setMaxDepth(0)
->setUlClass('nav navbar-nav') ?>
I get this error message via browser:
Fatal error: Uncaught Error: Class 'Application\view\helper\Menu' not found in C:\wamp64\www\xyz\vendor\zendframework\zend-servicemanager\src\Factory\InvokableFactory.php
I'd really love to understand this because it might be really helpful in future, so any suggestion is appreciated.
Move file Menu.php to the folder Application/src/Application/View/Helper

tax_query to get posts with a field with empty value?

I defined a custom field, to be able to hide posts from home, like this:
/* Campo adicional que puede ocultar noticia de la home */
function mnd_get_custom_field( $value ) {
global $post;
$custom_field = get_post_meta( $post->ID, $value, true );
if ( !empty( $custom_field ) )
return is_array( $custom_field ) ? stripslashes_deep( $custom_field ) : stripslashes( wp_kses_decode_entities( $custom_field ) );
return false;
}
function mnd_add_custom_meta_box() {
add_meta_box( 'home-page', __( 'Home page', 'mnd' ), 'mnd_meta_box_output', 'post', 'normal', 'high' );
}
add_action( 'add_meta_boxes', 'mnd_add_custom_meta_box' );
function mnd_meta_box_output( $post ) {
wp_nonce_field( 'my_mnd_meta_box_nonce', 'mnd_meta_box_nonce' ); ?>
<p>
<label for="mnd_hide_homepage"><?php _e( 'Hide from homepage', 'mnd' ); ?>:</label>
<input type="checkbox" name="mnd_hide_homepage" id="mnd_hide_homepage" checked="<?php echo mnd_get_custom_field( 'mnd_hide_homepage' ) == 1 ? 'checked' : ''; ?>" value="1" size="50" />
</p>
<?php
}
function mnd_meta_box_save( $post_id ) {
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if( !isset( $_POST['mnd_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['mnd_meta_box_nonce'], 'my_mnd_meta_box_nonce' ) ) return;
if( !current_user_can( 'edit_post', get_the_id() ) ) return;
if( isset( $_POST['mnd_hide_homepage'] ) )
update_post_meta( $post_id, 'mnd_hide_homepage', esc_attr( $_POST['mnd_hide_homepage'] ) );
}
add_action( 'save_post', 'mnd_meta_box_save' );
and I'm trying like:
// The Query
$ppp = 12;
$page = $_GET['page'];
$args_count = array(
'post_type' => 'post',
'paged' => false,
'status' => 'publish'
);
$posts_home_count = new WP_Query( $args_count );
$manyPosts = $posts_home_count->found_posts;
$paginas = $manyPosts / $ppp;
wp_reset_postdata();
$meta_params = array(
array(
'key' => 'mnd_hide_homepage',
'value' => 1,
'compare' => '!=',
'type' => 'NUMERIC'
)
);
$args = array(
'post_type' => 'post',
'posts_per_page' => $ppp,
'paged' => $page,
'page' => $page,
'status' => 'publish'/*,
'meta_query' => $meta_params */
);
//print_r ( $args );
$posts_home = new WP_Query( $args );
And it won't return any posts,
But if I change to
'compare' => '=',
Then it returns all the checked posts
Any idea what I'm missing?
There are some problems with Meta Query sometimes and the problem is not always visible. Try the following:
var_dump the WP_Query object and check the SQL manually. Do you see the problem?
Did you try to remove 'type' => 'NUMERIC'?
The “hack” solution (untested):
global $wpdb;
$ids = $wpdb->get_results("
SELECT $wpdb->posts.id FROM $wpdb->posts
WHERE post_status=publish
INNER JOIN $wpdb->post_meta
ON $wpdb->post_meta.post_id = $wpdb->posts.id
AND $wpdb->post_meta.key = 'mnd_hide_homepage'
AND $wpdb->post_meta.value != '1'
");
// put the ids with post__in into the WP_Query arguments
Maybe this is the solution. If not, it may help to find it.
Try this:
array(
'key' => 'mnd_hide_homepage',
'value' => '1',
'compare' => '!=',
'type' => 'NUMERIC'
)
Or
array(
'key' => 'mnd_hide_homepage',
'value' => array('1'),
'compare' => 'NOT IN'
)

Drupal 7 | Form managed file upload image preview

I'm can't get to work image preview on Drupal 7 Form managed file.
I have for code like this in template.php:
function testform($form, &$form_state) {
$form = array();
$form['con_image'] = array(
'#type' => 'managed_file',
'#title' => t('Image'),
'#required' => TRUE,
'#default_value' => variable_get('con_image', ''),
'#progress_indicator' => 'bar',
'#progress_message' => 'Uploading ...',
'#upload_location' => 'public://gallery/',
'#theme' => 'test',
'#upload_validators' => array(
'file_validate_is_image' => array(),
'file_validate_extensions' => array('jpg jpeg'),
'file_validate_image_resolution' => array('6000x4000','800x600'),
'file_validate_size' => array(6 * 1024 * 1024),
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Add to site'),
);
return $form;
}
I call testform after checking additional conditions (like userpoints) via code
$arr = drupal_get_form('testform');
print drupal_render($arr);
The form itself is working, I'm able to add image to node (programmatically) but cannot get the preview of image during upload process. I try to use #theme but it seems doesn't work at all.
My theme function looks like this:
function theme_test($variables) {
$element = $variables['element'];
$output = '';
$base = drupal_render_children($element); // renders element as usual
if($element['fid']['#value'] != 0) {
// if image is uploaded show its thumbnail to the output HTML
$output .= '<div class="multifield-thumbnail">';
$output .= theme('image_style', array('style_name' => 'thumbnail', 'path' => file_load($element['fid']['#value'])->uri, 'getsize' => FALSE));
$output .= '</div>';
}
Any ideas?
You need to declare your theme with a hook_theme in your module.
function yourmodule_theme() {
return array(
'test' => array(
'render element' => 'element',
)
);
}