Hot answers tagged

27

Since WordPress 3.5 this option (XML-RPC) is enabled by default, and the ability to turn it off from WordPress dashboard is gone. Add this code snippet for use in functions.php: // Disable use XML-RPC add_filter( 'xmlrpc_enabled', '__return_false' ); // Disable X-Pingback to header add_filter( 'wp_headers', 'disable_x_pingback' ); function ...


25

<?php // would echo post 7's content up until the <!--more--> tag $post_7 = get_post(7); $excerpt = $post_7->post_excerpt; echo $excerpt; // would get post 12's entire content after which you // can manipulate it with your own trimming preferences $post_12 = get_post(12); $trim_me = $post_12->post_content; my_trim_function( $trim_me ); ?&...


25

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


22

Here you go ! <?php $my_id = 5369; $post_id_5369 = get_post($my_id); $content = $post_id_5369->post_content; $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); echo $content; ?>


16

The content is a property of the post object, not of the query object. Use $post or get_post() instead: if( '' !== get_post()->post_content ) { // do something }


16

The wpautop() function adds <p> and <br> tags to your content in order to preserve line breaks. If you would rather add these tags yourself, then you can remove the filters which apply this function to the post content: remove_filter( 'the_content', 'wpautop' ); remove_filter( 'the_excerpt', 'wpautop' ); If you would like to keep the automatic ...


9

My way to do this (see update below): function addParagraphs($content) { // you can add as many as you want: $additions = array( '<p>After 1st paragraph</p>', '<p>After 2nd paragraph</p>' ); $content = get_the_content(); $output = ''; // define variable to avoid PHP warnings $parts = explode(...


9

Yep, wp_trim_words(): <?php $trimmed = wp_trim_words( $text, $num_words = 55, $more = null ); ?> Or in your case: <?php echo apply_filters( 'the_content', wp_trim_words( strip_tags( $post->post_content ), 55 ) ); ?>


9

How to trigger an oembed cache regeneration The default cache time is 24 hours and we can adjust it with the oembed_ttl filter. But as you've noticed, expired cache is not enough to trigger a cache regeneration. The reason is this line in the WP_Embed class: if ( $this->usecache || $cached_recently ) { so to trigger a regeneration, we need both ...


9

WordPress has a handy function called post_exists() which allows you to find an existing post by its title, content, and/or date. It returns the post ID on success or 0 otherwise. In your case, you can use $post_id = post_exists( '', '{"member_id":33}' ) to find the post you're looking for. Note: That function doesn't check the post type so you'd need to ...


8

Wordpress automatically ads the <p> tags to the content. So it shows up while loading the content. This is with the filter wpautop. So we will remove this filter for the image post type only. You can manage this by adding the following code in functions.php file. // Add the filter to manage the p tags add_filter( 'the_content', '...


7

I ended up using userabuser's answer with a small modification, because global $post doesn't seem to exist on init. You can instead just query for post in querystring, like so: function remove_editor() { if (isset($_GET['post'])) { $id = $_GET['post']; $template = get_post_meta($id, '_wp_page_template', true); if($template == '...


7

You can use the_content with an high prioriety (lower number). add_filter( 'the_content', function( $content ) { return 'Hello World '.$content; }, 0); You can even use negative priority: add_filter( 'the_content', function( $content ) { return 'Hello World '.$content; }, -10); Note that this will apply everytime 'the_content' is used, no matter the ...


7

First set the post type to display on main feed page i.e. /feed using pre_get_posts hook $q->set('post_type', array('post', 'page')); On individual page WordPress shows comment feed then set it to false and display page content in feed. $q->is_comment_feed = false; In feed template WordPress calls the_excerpt_rss() which calls get_the_excerpt() so ...


6

You already got something like this built in: Revisions. // Define the nr of saved revisions in your wp-config.php define( 'WP_POST_REVISIONS', 30 ); You can simply grab them by calling get_posts() with a post_type of revision. To show the difference between two revisions simply use wp_text_diff(). // Example $revisions = get_posts( array( 'post_type'...


6

If this post type is called "image", you can create a single template to handle the display of just the image post type. Just copy your 'single.php' file and rename the copy 'single-image.php'. Now you can control just the image posts. To strip out tags, I like to use the strip_tags() function. If you print the content of the post with the_content() it ...


6

If we look at the source of wp_insert_attachment() we find the hooks edit_attachment and add_attachment. In your case we could use the add_attachment hook. The callback takes the attachment's ID as a parameter. By looking at the database table wp_posts we see that the description is saved as post_content and the caption as post_excerpt: Then you could try ...


6

Prevent authors from publishing too short content: Here's one idea using a custom post status, for example short: /** * Register a custom 'short' post status * * @see http://wordpress.stackexchange.com/a/159044/26350 */ function wpse_short_post_status() { register_post_status( 'short', array( 'label' => _x( 'Short', '...


6

You are using get_the_content() wrong, it can't take a ID, which is what get_option('page_for_posts') does return, and generally gets the content of the current post inside the loop, in which it has to be used. To get the actual content of that page you can do: $page_for_posts_id = get_option( 'page_for_posts' ); $page_for_posts_obj = get_post( $...


6

EDIT After reviewing your edit, you may want to try following These Instructions To embed a video or another object into a post or page, place its URL into the content area. Make sure the URL is on its own line and not hyperlinked (clickable when viewing the post). It sounds like you would want to use wp_get_oembed() - The examples look like this: ...


6

We are using the htaccess file to protect it from hackers. # BEGIN protect xmlrpc.php <files xmlrpc.php> order allow,deny deny from all </files> # END protect xmlrpc.php


6

When you have the ability to block it via your web server's configuration, @Charles' suggestions are good. If you can only disable it using php, the xmlrpc_enabled filter is not the right way. Like documented here: https://developer.wordpress.org/reference/hooks/xmlrpc_enabled/ it only disables xml rpc methods that require authentication. Instead use the ...


6

You can use the_post hook to remove <!--nextpage-->. In this case: add_action( 'the_post', 'campaign_remove_nextpage', 99); function campaign_remove_nextpage ( $post ) { if ( ($_GET['utm_campaign']== 'nonextpagecampaign') && (false !== strpos( $post->post_content, '<!--nextpage-->' )) ) { // Reset the global $pages: ...


6

In your dashboard go to settings/permalinks. Hit save. You should be able to see your cpts now.


5

Actually, my recommendation would be to do things a bit differently. You can add a custom rewrite endpoint to WordPress to handle these files specifically. For example, the URL http://site.com/download-xml/the_filename would automatically download the specified file as an attachment. First, you need to add a custom rewrite endpoint to set this up: ...


5

You could use a Git hook and post per XML-RPC to WordPress. A Git hook can be any executable file, even PHP. Another option – on GitHub – is to use the email hook: Go to https://github.com/username/projectname/admin/hooks, select Email and send an email to the blog. Enable the Post per email feature.


5

What about if ( !empty( get_the_content() ) ){ //code }


5

You have to define WP_CONTENT_DIR and WP_CONTENT_URL: const WP_CONTENT_DIR = '/path/to/new/directory'; const WP_CONTENT_URL = 'http://content.wp'; The new path must be accessible for read and write operation from the WordPress core directory. You might need a helper function to add the new directory path to the open_basedir list: /** * Add a new ...


5

The internal implementation details of that feature are of questionable sanity. If you take a look at said featured-content.php template you would see that it get posts from twentyfourteen_get_featured_posts() however the only thing that function has is twentyfourteen_get_featured_posts filter from quick look at which in peculiar fashion nothing is actually ...


5

You can disable the visual editor entirely in your profile settings. If you need to disable it on a per page basis, take a look at this answer. Using text mode may not solve the empty paragraph problem, the wpautop filter is applied to the content regardless of how the content is edited. You can remove the filter, but then you'll have to manually break and ...


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