Tag Info

Hot answers tagged

41

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;


24

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


17

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

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


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

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


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


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

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


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

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


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


9

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


9

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


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

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

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


9

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


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


8

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


8

as a followup to t31os' answer, here's the complete code needed (combining the contents of the plugin mentioned with t31os' fix), with modifications to handle custom post types as well: add_filter( 'add_menu_classes', 'show_pending_number'); function show_pending_number( $menu ) { $type = "animals"; $status = "pending"; $num_posts = ...


8

<meta property="og:image" content="http://alexchen.info/taiwantalk/wp-content/uploads/2011/03/cheating.png" /> Visit http://developers.facebook.com/docs/reference/plugins/like/ and see "Step 2 - Get Open Graph Tags" There's also a lot of WP plugins out there that can help you to generate Open Graph tags.


8

add to your wp-config.php file the following line: // set max post revisions define('WP_POST_REVISIONS', 5); source: http://codex.wordpress.org/Editing_wp-config.php#Post_Revisions


8

Although having the feature of persistent settings in core is nice, it may take quite a while before it's actually accepted. WordPress 3.5 is still quite far away. So let's augment the global $_REQUEST array instead. add_action( 'load-edit.php', 'wpse34956_force_excerpt' ); function wpse34956_force_excerpt() { $_REQUEST['mode'] = 'excerpt'; } This ...


8

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


8

You are right on the mark with GET, that would probably be easiest to make use of. Try this: Add_Child_Page::on_load(); class Add_Child_Page { static function on_load() { add_action( 'init', array( __CLASS__, 'init' ) ); add_action( 'admin_init', array( __CLASS__, 'admin_init' ) ); } static function init() { ...



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