Hot answers tagged

73

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;


61

echo get_post_field('post_content', $post_id);


28

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 ...


25

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 specified * * ...


24

Use the page_for_posts option: <?php $page_for_posts = get_option( 'page_for_posts' ); ?> This will return the ID of the Page assigned to display the Blog Posts Index.


21

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 ) ...


19

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 ...


18

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


17

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: ...


16

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' );


15

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 ...


15

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 defaults are merged with 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 set ...


14

I don't think there exist one, but you could create your own, wpse_empty_trash, with something like this: /** * Add a custom hook 'wpse_empty_trash' */ add_action( 'load-edit.php', function() { add_action( 'before_delete_post', function ( $post_id ) { if ( 'trash' === get_post_status( $post_id ) && ...


13

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 ...


13

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 ...


13

When updating post taxonomies, you can call wp_defer_term_counting( true ) before you do your mass operation, and then wp_defer_term_counting( false ) to update the term counts only once per term.


13

The following code will work, but you just have to keep in mind that conflicts can happen easily if the slug for your custom post type is the same as a page or post's slug... First, we will remove the slug from the permalink: function na_remove_slug( $post_link, $post, $leavename ) { if ( 'events' != $post->post_type || 'publish' != ...


12

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; });


12

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 ...


12

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 ...


12

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 ...


12

The answer by shea is not ideal as in many cases: You don't want to strip everything from <br>, <p> etc. You want it as a default behavior for your WP visual composer which the above code will delete In many cases it is considered as "hacking the core" as this is changing the default core behavior of WP - for example such a thing will not pass ...


11

In the tutorial (Example 1), he has to declare the global $post so that he can access the post_parent from it. In a function like that, the $post is not a global variable unless he makes it so. In the codex (Example 2), it is declared global because the sample code is just a sample, explicitly trying to tell you that the $post variable is a global one. ...


11

There is a sad truth: you can never ever be sure that some code will not break your code, and there is nothing you can do to prevent that. Especially in WordPress, where everything is global. That said, yes, global $post is one of the most used global var, so using special care for it can be a good idea. In my code I rarely directly access global $post. ...


11

A useful fast way is to use JavaScript in combination with an Ajax action that outputs 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 * ...


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 ); ...


11

You can use the following instead, if (is_singular('post')) { //your code here... } Where by is_singular is the WordPress API conditional function for testing for the existence of a post type. You can also pass an array of post types if you wish. http://codex.wordpress.org/Function_Reference/is_singular


11

By default all custom post adds support for Title and editor, if you want more stuff like comments, thumbnail and revisions you have to add it manually in the support argument. Read more about how to register your custom post type here, you can also find the section about support to see what you can add. Here is an example wehere to register thumbnail for ...


11

This will do it: (add to your theme's functions.php) add_action( 'pre_get_posts', 'set_posts_per_page' ); function set_posts_per_page( $query ) { global $wp_the_query; if ( ( ! is_admin() ) && ( $query === $wp_the_query ) && ( $query->is_search() ) ) { $query->set( 'posts_per_page', 3 ); } elseif ( ( ! is_admin() ) ...


11

Hi Scott B: Hate to be the bearer of bad news but WordPress hardcodes the Page Template functionality to the "page" post type, at least in v3.0 (that might change in future versions but there's not a specific initiative I'm aware of to change it yet. So this is one of the very few times I'm struggling to figure out how to get around something without ...



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