wordpress missing P tag - wordpress

I am creating a file outside of wordpress but i want to get wordpress post etc. I created the following which does whar i want it to do but it is missing th P tags. if i view this post through wp-admin it looks fine. if i view it through this function it doe snot look find
ini_set('display_errors','on');
//Global WordPress
global $wpdb;
if(!isset($wpdb))
{
require_once('wp-config.php');
require_once('wp-load.php');
require_once('wp-includes/wp-db.php');
}
$args = array("post_title" => $_GET['name'],"category_name"=>"knowledge-map");
$query = get_posts( $args );
$posttitle = $_GET['name'];
$postid = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = '" . $posttitle . "'" );
$post = $getpost= get_post($postid);
$post_title = sanitize_post_field( 'post_title', $post->post_title, $post->ID, 'display' );
$post_content = sanitize_post_field( 'post_content', $post->post_content, $post->ID, 'display' );
//$postcontent= $getpost->post_content;
//setup_postdata( $getpost );
$data = $post->post_content;
//foreach ( $getpost as $post ) : setup_postdata( $post );
//$data = get_the_title() . the_content();
//endforeach;
wp_reset_postdata();
echo $data;

post_content won't show proper formatting...basically ever. You need to apply the_content filter to it:
echo apply_filters('the_content', $data);
Here's where it's mentioned in the Codex.
While wpautop will only add paragraph tags where new lines exist, the_content runs a bunch of core filters (including wpautop). From the source, the_content includes:
add_filter( 'the_content', 'wptexturize' );
add_filter( 'the_content', 'convert_smilies' );
add_filter( 'the_content', 'convert_chars' );
add_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'shortcode_unautop' );
add_filter( 'the_content', 'prepend_attachment' );

you can also use wpauto, it uses in wordpress for auto formatting of text
echo wpautop( $post_content );

Not very techy, but I found that if I copy everything from the "text" area in a Wordpress post, then paste it into a markdown to HTML editor, then it results in tags being added wherever Wordpress omits them!
Copy the "Text" from a Wordpress post
Paste into markdown/HTML editor
View the HTML for that markdown

Related

WordPress : How to remove unecessary breakline<br> tags in <p> and <ul><li> tags

Hi I have this wordpress site, and I want to remove the unnecessary breakline tag that automatically inserts into the <ul><li> tag this is the code that I insert it in functions.php file:
remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );
add_filter( 'the_content', 'nl2br' );
add_filter( 'the_excerpt', 'nl2br' );
add_filter('the_content', 'remove_empty_p', 20, 1);
function remove_empty_p($content){
$content = force_balance_tags($content);
return preg_replace('#<p>\s*+(<br\s*/*>)?\s*</p>#i', '', $content);
}
The nl2br will breakline the_content on it. But it seems that <ul><li>
also inserts a breakline to it.
I want that only <p> tag will breakline.
My problem is how will I be able to prevent that it wont add a breakline inside <ul><li> tag?
Can anyone help me on this?
Any help is much appreciated. TIA
Try below code
add_filter('tiny_mce_before_init', 'override_mce_options');
function remove_empty_p( $content ) {
$content = force_balance_tags( $content );
$content = preg_replace( '#<p>\s*+(<br\s*/*>)?\s*</p>#i', '', $content );
$content = preg_replace( '~\s?<p>(\s| )+</p>\s?~', '', $content );
return $content;
}
add_filter('the_content', 'remove_empty_p', 20, 1);
remove_filter( 'the_content', 'wpautop' );
remove_filter( 'the_excerpt', 'wpautop' );

Woocommerce - Print Product's Custom Field In Email

In Woocommerce, I need to print a custom field on the Completed Order email.
The template is completely unique in my themes woocommerce/emails folder because the default template doesn't work for my needs.
As such I'm trying to display the following:
Title, Quantity and two custom fields
This needs to be included in the customer-completed-order.php email template but I am not certain what syntax is necessary to print it.
I found something by Googling but it doesn't work:
<?php foreach ( $items as $item_id => $item ) :
$_product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
?>
<p>Class: <?php echo get_post_meta($_product->id,'name',true); ?></p>
<p>Date: <?php echo get_post_meta($_product->id,'date_text_field',true); ?></p>
<p>Time: <?php echo get_post_meta($_product->id,'time_text_field',true); ?></p>
<p>Number of Spaces: <?php echo get_post_meta($_product->id,'qty',true); ?></p>
<?php endforeach; ?>
Try adding to action hook,
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order );
OR
do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order );
This could be used in following way:
add_action( 'woocommerce_order_item_name', 'action_custom_order_meta', 10, 3 );
OR
add_action( 'woocommerce_order_item_name', 'action_custon_order_meta', 10, 3 );
Following is the callback handler function.
function action_custom_order_meta($item_name, $item, $false){
$product_id = $item['product_id'];
$product_custom_date = get_post_meta($product_id, 'product_custom_date', true);
$product_custom_time = get_post_meta($product_id, 'product_custom_time', true);
$product_custom_meta = 'Date: ' . $product_custom_date . ' <br> Time: ' . $product_custom_time;
echo $product_custom_meta;
}
Change the custom field names to appropriate. You might also need to wrap the output according to the required markup for your custom email.
Hope this helps.

Add current_cat class on wp_list_categories when I'm on single with custom taxonomy

I search all the web for that answer. I use wp_list_categories to make a submenu with custom taxonomy, It works well, and puts current-cat when I browse those categories.
The thing is, when I browse single posts with this menu, the highlight no more works.
For the blog part of that site, I use the following code to highlight current category on wp_list_categories():
function sgr_show_current_cat_on_single($output) {
global $post;
if( is_single() ) {
$categories = wp_get_post_categories($post->ID);
foreach( $categories as $catid ) {
$cat = get_category($catid);
if(preg_match('#cat-item-' . $cat->cat_ID . '#', $output)) {
$output = str_replace('cat-item-'.$cat->cat_ID, 'cat-item-'.$cat->cat_ID . ' current-cat', $output);
}
}
}
return $output;
}
add_filter('wp_list_categories', 'sgr_show_current_cat_on_single');
But as far as I tried, can't make it work for single posts that are ordered by custom taxonomy. :/ > I don't know how to custom it.
Is it even possible ?
You need to use get_the_terms( $id, $taxonomy ); instead of wp_get_post_categories(); for getting custom taxonomy term IDs.
You can hardcode taxonomy name into the functon or get it from the $args you passed into wp_list_categories( $args );.
Final code:
add_filter( 'wp_list_categories', 'sgr_show_current_cat_on_single', 10, 2 );
function sgr_show_current_cat_on_single( $output, $args ) {
if ( is_single() ) :
global $post;
$terms = get_the_terms( $post->ID, $args['taxonomy'] );
foreach( $terms as $term ) {
if ( preg_match( '#cat-item-' . $term ->term_id . '#', $output ) ) {
$output = str_replace('cat-item-'.$term ->term_id, 'cat-item-'.$term ->term_id . ' current-cat', $output);
}
}
endif;
return $output;
}

loop won't work inside my plugin

Is it possible to loop in a wordpress plugin?
I've created this plugin that utilizes a wordpress loop to grab some info about posts in my custom post type of events:
function getEventsFeed() {
$args = array( 'post_type' => 'events' );
$loop = new WP_Query( $args );
$htmlOutput = '<h2>Events</h2>';
while ( $loop->have_posts() ) : $loop->the_post();
$date = get_post_meta($post->ID, 'events_0');
$location = get_post_meta($post->ID, 'events_9');
$htmlOutput .= '<tr><td>' . the_title() . '</td><td>' . $date[0] . '</td><td>' . $post->post_title .'</td><td>' . $location[0] . '</td></tr>';
endwhile;
$htmlOutput .= '</div>';
echo $htmlOutput;
}
Problem is only the the_title info is being returned. $post is not working within the loop so $post->ID and $post->post_title are not being returned. I'm using this exact code in another page template and it returns all the data correctly. I'm not sure why it won't return when I use it in a plugin.
Any ideas?
Try adding
global $post;
to the beginning of your function. $loop->the_post() will set the global $post variable, but it is not available inside your function scope.

wp_editor api not writing p tags in custom user profile field

I am building a custom biography field in the wordpress user profile page using WP's wp_editor api (using WP version 3.3.1).
When I add any content though, the WSYIWYG editor is not writing the paragraph tags to the database.
I am able to use the visual editor to add all other mark-up (bold, italics, etc) & I can manually add paragraph tags and save the full marked-up text to the database.
Has anyone else run into this issue? Code I'm using to build the custom field in the functions.php file below.
//ADD EXTRA FIELDS TO USER PROFILE
add_action( 'show_user_profile', 'extra_profile_fields' );
add_action( 'edit_user_profile', 'extra_profile_fields' );
function extra_profile_fields( $user ) {
//CHECK FOR AUTHOR BIO CONTENT
$check_for_bio = get_the_author_meta('authorbio', $user->ID);
$author_bio = '';
if (!empty($check_for_bio)) { $author_bio = $check_for_bio; }
//BUILD THE INPUT
echo '<table class="form-table">';
echo '<tr>';
echo '<th><label for="authorbio">Author Biography</label></th>';
echo '<td>';
$settings = array('wpautop' => true, 'media_buttons' => false);
wp_editor( $author_bio, 'authorbio', $settings);
echo '</td>';
echo '</tr>';
echo '</table>';
}
//SAVE EXTRA FIELDS TO USER PROFILE
add_action( 'personal_options_update', 'save_extra_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );
function save_extra_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
update_user_meta( $user_id, 'authorbio', $_POST['authorbio'] );
}
Solved the issue. Was not adding the_content fitler. When printing the output.
<?php
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
?>
http://codex.wordpress.org/Function_Reference/the_content
For PHP dummies like I am, this is a possible way how to create an ouput:
<?php
$bio_czech = get_the_author_meta('bio_czech',$user->ID);
echo wpautop($bio_czech);
?>

Resources