Find the comment's post title - wordpress

I am trying to create a plugin what will be a sime kind of a comments archive.
I have looked at get_comment().
I am looking to show where the comment came from, specifically the title of the post where the comment is attached. This is part of my code:
if ( $comments ) {
foreach ( $comments as $comment ) {
// here you can display the comment in the way you want
echo 'from: ' . $comment->i want comment post title here . '<br/>';
echo '<p>' . $comment->comment_content . '</p>';
}
}
How to do this?

Here is how you do it:
<?php
if ( $comments )
foreach($comments as $comment){
// here you can display the comment in the way you want
echo 'from: ' . get_the_title($comment->comment_post_ID) . '<br/>';
echo '<p>' . $comment->comment_content . '</p>;
}
?>

Related

Same link returns everytime by using multiple excerpts

On the main page in Wordpress I use multiple excerpts of different pages. The problem is though that it returns everytime the same excerpt link. It seems like the last link (of 3) on the page is used everytime.
The code (of course I changed the number of the id everytime ;-) :
<?php
$post_id = 35; // post id
$queried_post = get_post($post_id);
$my_excerpt = get_excerpt_by_id_long($queried_post); //$post_id is the post id of the desired post
echo '<a href="' . get_permalink( $queried_post ) . '" title="' . $queried_post->post_title . '">';
echo '<h3><strong>';
echo $queried_post->post_title;
echo '</strong></h3>';
echo '</a>';
echo $my_excerpt;
?>
This is in functions.php
function get_excerpt_by_id($post_id){
$the_post = get_post($post_id); //Gets post ID
$the_excerpt = $the_post->post_content; //Gets post_content to be used as a basis for the excerpt
$excerpt_length = 35; //Sets excerpt length by word count
$the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images
$words = explode(' ', $the_excerpt, $excerpt_length + 1);
if(count($words) > $excerpt_length) :
array_pop($words);
array_push($words, '…');
$the_excerpt = implode(' ', $words);
endif;
$the_excerpt = '<p>' . $the_excerpt . '<a class="leesmeer" href="'.get_permalink($post_id).'">lees verder...</a></p>';
return $the_excerpt;
}
never mind, I found the solution: forgot to use the $post_id in the .get_permalink()...Edited the code in the above functions.php

Insert custom content in WooCommerce

I have some problem in WooCommerce Admin Order menu. I want to add some content in the column but I don't know where I can make it.
This is some example screenshoot about my woocommerce admin order view :
I want to add phone number bellow email address in Order column.
Please help me to do that.
Thank you.
I've had to do this once, i've followed this tutorial :
https://code.tutsplus.com/articles/add-a-custom-column-in-posts-and-custom-post-types-admin-screen--wp-24934
add_action('manage_shop_order_posts_custom_column', 'match_order_woocommerce_custom_order_columns', 2);
function match_order_woocommerce_custom_order_columns( $column ) {
remove_action('manage_shop_order_posts_custom_column', 'woocommerce_custom_order_columns', 2);
global $post, $woocommerce;
$order = new WC_Order( $post->ID );
switch ($column) {
case "order_title" :
if ($order->user_id) $user_info = get_userdata($order->user_id);
if (isset($user_info) && $user_info) :
$user = '<a href="user-edit.php?user_id=' . esc_attr( $user_info->ID ) . '">';
if ($user_info->first_name || $user_info->last_name) $user .= $user_info->first_name.' '.$user_info->last_name;
else $user .= esc_html( $user_info->display_name );
$user .= '</a>';
else :
$user = __('Guest', 'woocommerce');
endif;
echo '<strong>'.sprintf( __('Order %s', 'woocommerce'), $order->get_order_number() ).'</strong> ' . __('made by', 'woocommerce') . ' ' . $user;
if ($order->billing_email) :
echo '<small class="meta">'.__('Email:', 'woocommerce') . ' ' . ''.esc_html( $order->billing_email ).'</small>';
endif;
if ($order->billing_phone) :
echo '<small class="meta">'.__('Tel:', 'woocommerce') . ' ' . esc_html( $order->billing_phone ) . '</small>';
endif;
break;
}
}
Please try this snippet in your active theme's functions.php

Escaping untrusted data in php for my wordPress theme

I have got a comment from someone superior to me who wants me to escape some comment in two php snippets which I have posted below. The problem is I don't know how to do that. Can anyone help me by modifying the snippets.
Comment I got:
Comment #1:
Validate and/or sanitize untrusted data before entering into the database. All untrusted data should be escaped before output.
Code Snippet #1:
<?php
if ( get_header_image() && !('blank' == get_header_textcolor()) ) {
echo '<div class="site-branding header-background-image" style="background-image: url(' . get_header_image() . ')">';
} else {
echo '<div class="site-branding">';
}
?>
Code Snippet #2:
<?php
printf(
/* translators: %1$s = text link: sangeet, URL: http://wordpress.org/themes/sangeet/, %2$s = text link: Kiran Kumar Dash, URL: https://twitter.com/TheKiranDash */
__( 'Theme: %1$s by %2$s', 'sangeet' ),
'' . esc_attr( 'sangeet', 'sangeet' ) . '',
'' . esc_attr__( 'Kiran Kumar Dash', 'sangeet' ) . '' );
?>
Comment #2:
esc all get_permalink() in content.php
Code snippet #3
<?php
if ( !is_single() ) {
echo '<div class="index-box">';
if ( has_post_thumbnail()) {
echo '<div class="small-index-thumbnail clear">';
echo '<a href="' . get_permalink() . '" title="' . __('Read ', 'sangeet') . get_the_title() . '" rel="bookmark">';
echo the_post_thumbnail('index-thumb');
echo '</a>';
echo '</div>';
}
}
?>
My approach:
I used esc_url to esc the get_permalink() in the snippet. Shall I use esc_all? Or esc_url is just fine.
<?php
if ( !is_single() ) {
echo '<div class="index-box">';
if ( has_post_thumbnail()) {
echo '<div class="small-index-thumbnail clear">';
echo '<a href="' . esc_url(get_permalink()) . '" title="' . __('Read ', 'sangeet') . get_the_title() . '" rel="bookmark">';
echo the_post_thumbnail('index-thumb');
echo '</a>';
echo '</div>';
}
}
?>
As requested:
PS. I didnt write this, only advanced it.
Usage:
$user = sanctify($_GET['user']);
Function:
function sanctify($data){
// Fix &entity\n;
$data = str_replace(array('&','<','>'), array('&amp;','&lt;','&gt;'), $data);
$data = preg_replace('/(&#*\w+)[\x00-\x20]+;/u', '$1;', $data);
$data = preg_replace('/(&#x*[0-9A-F]+);*/iu', '$1;', $data);
$data = html_entity_decode($data, ENT_COMPAT, 'UTF-8');
// Remove any attribute starting with "on" or xmlns
$data = preg_replace('#(<[^>]+?[\x00-\x20"\'])(?:on|xmlns)[^>]*+>#iu', '$1>', $data);
// Remove javascript: and vbscript: protocols
$data = preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([`\'"]*)[\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2nojavascript...', $data);
$data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2novbscript...', $data);
$data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#u', '$1=$2nomozbinding...', $data);
// Only works in IE: <span style="width: expression(alert('Ping!'));"></span>
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?expression[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?behaviour[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*+>#iu', '$1>', $data);
// Remove namespaced elements (we do not need them)
$data = preg_replace('#</*\w+:\w[^>]*+>#i', '', $data);
$data = str_replace('"','',str_replace("'","",$data));
do{
// Remove really unwanted tags
$old_data = $data;
$data = preg_replace('#</*(?:applet|b(?:ase|gsound|link)|embed|frame(?:set)?|i(?:frame|layer)|l(?:ayer|ink)|meta|object|s(?:cript|tyle)|title|xml)[^>]*+>#i', '', $data);
}
while ($old_data !== $data);
// we are done...
return $data;
}

wordpress acf form display data orderby order_no

For ACF form plugin, I used field group function which contains 10 fields, and loop to display in post like below:
<?php $fields = get_field_objects($post_id);
if( $fields)
{
foreach( $fields as $field_name => $field )
{
if($field['type']=='text' || $field['type']=='textarea' ){
echo '<li>';
echo '<label>' . $field['label'] . '</label>';
echo '<span>' . $field['value'] . '</span>';
echo '<span>' . $field['order_no'] . '</span>';
echo '</li>';
}
}
}?>
Now I want to sort it by order_no, how can I do it?
You could try something like that before you print out:
function objectSort( $a, $b ) {
return $a->order_no == $b->order_no ? 0 : ( $a->order_no > $b->order_no ) ? 1 : -1;
}
usort( $fields, 'objectSort' );
Note: This is not meant to be a full correct code, I just want to give you an idea for the solution.

Creating a recent posts widget for wordpress but ran into problems where it displays info from the first post

Hey guys I am making a Widget to display a posts title, and excerpt from that post, and its date. The code I have is here:
public function widget( $args, $instance ) {
extract( $args );
$headline = $instance['headline'];
$category = $instance['category'];
$numberposts = $instance['numberposts'];
$readmore = $instance['readmore'];
echo $before_widget;
echo $before_title;
echo "<p class=\"headline\">$headline</p>";
echo $after_title;
$args = array( 'numberposts' => $numberposts, 'category_name' => $category );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<a href="' . get_permalink($recent["ID"]) . '" title=" '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> ';
echo the_time('F j, Y');
echo the_excerpt();
}
As you can see I am trying to call the time and the excerpt from the post. It is working but it is only displaying the date and and excerpt from the very first post that is called welcome. I want it to display the Date and excerpt from each individual post. I will post a link to the site with the widget in the side bar. Sorry if I am not being clear enough or if more info is needed I am very new to this.
http://www.modmacro.us/wpsandbox/
First thing: the_excerpt(), is already an echo statement. Not a return statement. It's the same with the_date(). Prefix them with "get_", and they will return the information you need, or you can get rid of the echo command preceding it.
Second thing: In order for those functions to work, they need to be used in a Wordpress loop.
Since we're already in a loop, we just have to make sure it changes the Wordpress Globals appropriately to reflect the information you need..
foreach( $recent_posts as $recent ){
setup_postdata(get_post($recent['ID']));
echo '<a href="' . get_permalink() . '" title=" '.esc_attr(get_the_title()).'" >' . get_the_title().'</a> ';
echo get_the_time('F j, Y', $recent['ID']);
the_excerpt();
}
wp_reset_postdata();

Resources