Tag Info

Hot answers tagged

49

Simple as it gets $my_postid = 12;//This is page id or post id $content_post = get_post($my_postid); $content = $content_post->post_content; $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); echo $content;


28

echo get_post_field('post_content', $post_id);


26

here's what we did yesterday on a client site that we were having this exact problem with... I created a quick filter as a plugin and activated it. <?php /* Plugin Name: Image P tag remover Description: Plugin to remove p tags from around images in content outputting, after WP autop filter has added them. (oh the irony) Version: 1.0 Author: Fublo Ltd ...


21

Hi @Robin I Knight If you read the documentation for wp_insert_post, it returns the post ID of the post you just created. If you combine that with the following function __update_post_meta (a custom function I acquired from this site and adapted a bit) /** * Updates post meta for a post. It also automatically deletes or adds the value to field_name if ...


16

Massively improved function developed for plugin heavy on images: if ( ! function_exists( 'get_attachment_id' ) ) { /** * Get the Attachment ID for a given image URL. * * @link http://wordpress.stackexchange.com/a/7094 * * @param string $url * * @return boolean|integer */ function get_attachment_id( $url ) ...


16

I believe the best way to do this in a plugin is to run the following sample function when the pre_get_posts action hook is encountered. The $wp_query object is available, meaning your conditional tags are available, but before WordPress gets the posts, which means you are changing query vars prior to the first query being run, rather than adding a second ...


15

is_home() checks for the "Posts Page", despite the somewhat confusing function name.


14

Create a file install.php in your wp-content directory. In that file you declare the wp_install_defaults() function. If you just leave that function empty no default categories, links, posts or pages will be created. The default (pluggable) wp_install_defaults() function can be found in wp-admin/includes/upgrade.php. Also see: ...


13

If you are talking about the answer i posted here its simply uploading file in an iframe to achieve "Ajax like" submit. Now if you already have a form that handles the post submit you can simply add the upload file field input somewhere in your form: <form ... ... <input type="file" name="thumbnail" id="thumbnail"> ... ... </form> make ...


12

Updating the posts $all_posts = get_posts( 'posts_per_page' => -1, 'post_type' => 'post' ); foreach ( $all_posts as $single ) { wp_update_post( array( 'ID' => $single->ID, 'post_title' => to_title_case( $single->post_title ) // see function below )); } Converting a string to "Title Case" And, while not ...


12

Basically you need to make WordPress treat img like block-level element for the purpose of formatting. Such elements are hardcoded in wpautop() and list is unfortunately not filtered. What I would do is: Fork wpautop() under different name. Add img to regexp in $allblocks variable. Remove wpautop from the_content filter. Add your forked version to ...


11

assuming you're using post_class(): add_filter('post_class', function($classes){ global $wp_query; if(($wp_query->current_post + 1) == $wp_query->post_count) $classes[] = 'last'; return $classes; });


11

First you have to get the images. How to get all images of a gallery is described here. WordPress uses two classes for unzipping files. The PHP bilt in ZipArchive() (usage see David Walsh). And PclZip, you can found this class in wp-admin/includes/class-pclzip.php. If you got problems with ZipArchive() try the PclZip class. Now you just to have to glue ...


11

My approach. No extra function, no filter. :) <div <?php post_class( 0 === ++$GLOBALS['wpdb']->current_post % 3 ? 'third' : '' ); ?>> Alternative: <div <?php post_class( 0 === ++$GLOBALS['wpdb']->wpse_post_counter % 3 ? 'third' : '' ); ?>>


11

You can add an endpoint to your URIs to handle special requests. Here is a basic example as plugin. To understand what's going on read Christopher Davis's fantastic tutorial A (Mostly) Complete Guide to the WordPress Rewrite API. <?php # -*- coding: utf-8 -*- /** * Plugin Name: T5 Endpoint Example * Description: Adds a permalink endpoint to posts ...


11

This requires PHP 5.3+ (WP requires PHP 5.2.4+) add_filter('get_the_excerpt', 'end_with_sentence'); function end_with_sentence($excerpt) { $allowed_end = array('.', '!', '?', '...'); $exc = explode( ' ', $excerpt ); $found = false; $last = ''; while ( ! $found && ! empty($exc) ) { $last = array_pop($exc); $end = strrev( $last ); ...


10

If you're on a remote server, XMLRPC would be best, but requires login details If you're in a PHP script on the same server, wp-load.php would be best ( XMLRPC will involve a request of sorts ) If you're in a bash or CLI script, WP CLI would be best, e.g.: wp post create --post_type=page --post_status=publish --post_title='A future post' ...


10

You kind of answered the question yourself already, Create a function that will create the 3 posts ex: function create_new_user_posts($user_id){ if (!$user_id>0) return; //here we know the user has been created so to create //3 posts we call wp_insert_post 3 times. // Create post object ...


10

Although I do not understand the motivation for truncating posts, I think this exercise is valuable for you to understand how to use cron + WordPress. Create a function to truncate posts This can be used for both methods below WP-cron or UNIX cron. function foobar_truncate_posts(){ global $wpdb; # Set your threshold of max posts and post_type ...


10

paste this in your theme function.php file add_filter('pre_get_posts', 'Per_category_basis'); function Per_category_basis($query){ if ($query->is_category) { // category named 'books' show 12 posts if (is_category('books'){ $query->set('posts_per_page', 12); } // category With ID = 32 show only 5 posts ...


10

<?php $postTitle = $_POST['post_title']; $post = $_POST['post']; $submit = $_POST['submit']; if(isset($submit)){ global $user_ID; $new_post = array( 'post_title' => $postTitle, 'post_content' => $post, 'post_status' => 'publish', 'post_date' => date('Y-m-d H:i:s'), 'post_author' => ...


10

It seems that the easiest way is to do it manually in the DB. If you don't have access to the DB, or if you don't want to do it manually, you can install a plugin that will do it one post at a time, or a plugin that will bulk convert a lot of posts at once


9

Hi @Robin I. Knight: I view get_the_excerpt() as a function with legacy design. As WordPress usage has grown there are many newer use-cases where it doesn't fit but where the newer functions for getting different data do. One example is the now frequent use of an $args array of function options. But it's easy to fix for your needs. Here's an alternative ...


9

function my_manage_columns( $columns ) { unset($columns['date']); return $columns; } function my_column_init() { add_filter( 'manage_posts_columns' , 'my_manage_columns' ); } add_action( 'admin_init' , 'my_column_init' );


9

You may notice that I did more or less exactly this for Matt's site: http://ma.tt. Every set of posts is grouped by the day. The basic principle is to keep track of your day in the loop, then print the date and related stuff only when it changes. Take a basic Loop: if ( have_posts() ) : while ( have_posts() ) : the_post(); the_title(); the_content(); ...


9

What (exactly) happens When calling the_content() inside your template, you are able to call it without any parameters. This means, that the function already has the defaults of null for both arguments: The "more" link text and the boolean switch that allows you to strip teaser content before the "more" link text. The the_content() function calls ...


9

You can do this with the Rewrite API's add_rewrite_endpoint: add_action( 'init', 'wpse51444_endpoint' ); function wpse51444_endpoint(){ add_rewrite_endpoint( 'related', EP_ALL ); } add_filter( 'query_vars', 'wpse51444_query_vars' ); function wpse51444_query_vars( $query_vars ){ // add related to the array of recognized query vars $query_vars[] ...


9

I understand that post ID's are unique, but are they reliable as persistent data ID's? Yes.


9

A useful fast way is use javascript in combination with an ajax action that output the box. Create a small plugin. Create a folder inside plugins directory, naming it 'SubmitReviewConf'. In this folder create a php file named SubmitReviewConf.php. in this file, put this code: <?php /** * Plugin Name: Submit for Review Confirmation Message * Description: ...


9

Every post update in wordpress, is handled by the wp_update_post function. This function has some defaults, and for post_content_filtered the default value is '' (empty string). Once the default are merged to args passed to function via wp_parse_args it means that every time a post is updated and post_content_filtered is not explicitly passed, it is setted ...



Only top voted, non community-wiki answers of a minimum length are eligible