Alter WordPress post title and body in view mode only - php

I am looking for the way how to alter a WordPress post title and body when it is displayed to a normal user. But not in any other context, ie in admin module.
The task at hand is to fetch a query parameter, carry out some calls to external webservice and database operations and replace some text in both title and body. The <title> tag should also contain the changed title.
I do not look for a plugin solution, but rather were I should include a php file with all the code that does all the necessary processing.
My expectation was that I don't have to deeply meddle with the theme files. Of course, I could inject my code in the respective theme's page.php or content.php file. But I'd like to do it in less intrusive way, for example by including my functions_custom.php file in the top of functions.php, and this file would contain a collection of functions and add_filter() calls to hook those functions on certain events. Is that possible to achieve my goal that way?
This is my first attempt:
<?php
function alter_title( $title, $id = null ) {
return 'Altered title: ' . $title;
}
add_filter( 'the_title', 'alter_title', 10, 2 );
?>
This causes change in all the post titles in all contexts, including the post list in the Dashboard. But I need it to be altered only in view context (this also does not influence the text used in the <title> tag). Perhaps I can sniff this context somehow, and engage the alteration only when the view mode is detected?

While ago I needed to do similar thing and my fast and short workaround was to have an "API" (
Created a template for that specific post type and using PHP generated a post ID indicator:
$post = get_post(); echo "var post_id = '".$post->ID."';";
I created a custom API that "takes" $_POST request with "post_id" param and it would fetch adequate/custom (in your case a content that will be shown to the user) content from database for the post with the post_id from $_POST request.
Then for every post there would be an ajax request to that API which sends post_id and fetches all the needed info (in your case post title and body) and then using eg. jQuery
.append() would populate post title with response post title and body too, respectively.
Also, if you'd like to avoid a extra request than you should try to generate that 'user-visible' content with PHP. That would mean you need to collect the content before loading the page and echoit in the place of default content.
I suppose you should create a function in functions.php that takes an $post->ID and fetches title and body from the database. Then you should call that function on every loop and single post template, if you understand me.

is_admin() will return true if you are viewing an admin page.
So you could do this:
function alter_title( $title, $id = null ) {
if(is_admin()){
return $title;
}else{
return 'Altered title: ' . $title;
}
}
add_filter( 'the_title', 'alter_title', 10, 2 );

Related

How to write a shortcode with variable to display content from specific custom post type post?

I've only written very basic shortcodes without variables and am just not figuring out how to write one that allows someone to enter a variable to pull info from a specific post.
I have a custom post type called "Events". I want to put a shortcode on the site FrontPage that will display the contents of a specific event within that custom post type. I'm assuming a variable is the way to do this, but I don't know what the best way is. Should I be having the user find the post ID number to use as the variable? Note that I am not trying to display an arcive of the post type, only the contents of a specific post, as indicated by the shortcode variable. I can see something like the following, but don't know how to achieve it:
[display-event id="77"]
Really, this is advanced for me, so any direction you can give me would be much appreciated.
~Laura
Have a look at WordPress's documentation for add_shortcode().
You would need to add something like the following in your functions.php file:-
function baztag_func( $atts, $content = "" ) {
// What you would like your short code to do
return "content = $content";
}
add_shortcode( 'baztag', 'baztag_func' );
add_shortcode

Wordpress - use external content

I would like to use external HTML as the content of some pages of my Wordpress site.
I.e., currently I have the html directly into the page in wordpress. However, I don't like this because I like to use an editor that has syntax highlighting, search/replace etc.
I was wondering if there was a way through the functions.php file to call content from an external HTML file to be inserted to the page content.
( I don't want to use javascript/jquery. I already have that working effectively, but I want to know if there is a way through php. )
UPDATE - ANSWER
After following instructions from both links (#zipkundan, #pierre), this was the final code I put together that works like a charm:
// add the filter call which will change the content
add_filter( 'the_content', 'insert_html' );
// the callback function - it gets the variable $content which holds default content
function insert_html($content) {
// I needed the content filtered by page - only for the 'help' page
if( is_page( 'help' )) {
// this line gets the html from the file - stores into myHtml var
$myHtml = file_get_contents(url_to_file);
return $myHtml;
}
// if the page is not 'help', we need to return the content, otherwise page will be empty
return $content;
}
Probably this could help you The "the_content" filter

PHP WordPress the_content()

Where can I find the file where the_content() gets the value from post_content in the database?
I wanted to do the concept but with another content from the database,
So far, I've added a new column to the database ex. post_content2
How can I get its value by using something like
<?php the_content2(); ?>
I tried looking at post-template.php and found the function but not how it gets the value from the database post_content.
From the comments above, and a more complete answer:
Do not modify core WP tables. Either use post_meta functions, or add a new table.
the_content() is just a convenience function that directly accesses the posts table, content column. You can do the same thing at any time by getting the value from the database, and passing it through apply_filters('the_content', $my_custom_content);. If you want to write your own the_content2() function, it could / should be done like so (using the post_meta method mentioned above):
function the_content2() {
// Global in the current post so we can get the ID
global $post;
// Load your custom content
$my_content = get_post_meta($post->ID, 'my_custom_content');
// Echo it out (like the_content()) applying 'the_content' filters
echo apply_filters('the_content', $my_content);
}
If you want to figure out how to SAVE this content, then you can read this writeup:
How to Create Custom Meta Boxes in WordPress
edit
It's worth noting that this functionality is already available in a few different plugins you can install. In my experience Custom Field Suite is the most reliable and easy to use (but that's just my opinion).

Insert variable into Wordpress page title for individual page

I have a photo gallery page that is using a single page in Wordpress.
The gallery display is dynamic, and relies on a URL parameter. The parameter itself is an integer of the relating wordpress page ID. (as that's the page it's about).
Here is an example of a gallery URL:
http://www.bellavou.co.uk/gallery/?pid=238
For SEO purposes, I'm trying to get away from every possible Gallery URL combination to have the page title 'Gallery', as is set in the Wordpress page settings.
I've been trying this, which has been mentioned a few times:
function assignPageTitle(){
return "Title goes here";
}
add_filter('wp_title', 'assignPageTitle');
But as I'm also using Yoast SEO plugin, I think this is over-writing it (even though I uncheck 'Force title rewrite', and keep the page title field blank). Not sure why the above function doesn't seem to work.
Within the gallery page, I'm able to show the h1 title properly, passing PHP Variables into the code, but I want to do the same to the page title, but in a way that can be done directly on the page template.
Why is it that whenever I post a question, I seem to find the answer very soon afterwards?
I read a post saying that it helps to put a wp_title function before the wp_header call, which is what I tried, and that seems to work fine.
I can edit the page title just for an individual page using the PHP page template, and using this:
$procedure = isset($_GET['pid']) ? $_GET['pid'] : '';
add_filter( 'wp_title', 'wp_title_gallery', 100 );
function wp_title_gallery($title) {
global $procedure;
if(!$procedure) {
$title = the_title();
} else {
$title = the_title(get_the_title($procedure).'&nbsp');
}
return $title;
}
get_header();
$procedure gets the URL parameter, and then the function takes that ID, and gets the associated page from it, and then prints the title of the page, into the page title hook. Lastly, the WP header is called.
(Just incase anyone comes here looking for a solution)

How to save the content of shortcode in wordpress options?

I am developing a plugin with Wordpress. But I need to save a short code like [my-shortcode] and then in my front end show the final result of the short code. Imagine that I have one form where you can upload an image, select width, height and preloaded themes, animations, etc. But I want to use short code.
How to save the compiled short code with do_shortcode() with Wordpress options?
I think, maybe I can do it with javascript, but I didn't found it on the web.
You cannot save a shortcode with do_shortcode(). do_shortcode()is used to render a shortcode.
You can use a metabox that renders an input and then you can use your JavaScript code that targets that input and stores your shortcode in its value attribute.
You can then refer to that post meta data which holds your shortcode by using the get_post_meta(); method provided by WordPress.
Here is the link to get_post_meta http://codex.wordpress.org/Function_Reference/get_post_meta
You can refer to how to add a metabox in WordPress here:
http://codex.wordpress.org/Function_Reference/add_meta_box
Your input tag should be coded like so in your callback function:
echo '<input type="hidden" id="My-Shortcode" name="My-Shortcode" ' . get_post_meta($post->ID, 'My-Shortcode', true) . '/>';
The input with the name My-Shortcode will actually hold your shortcode but as you see it requires a $post object which actually hold all the data for that post. You can then refer to your shortcode key by stating
get_post_meta($post->ID, 'My-Shortcode', true)
True simply means the value will be echoed out.
Now in order to access any data within that $post object you need to pass the $post parameter to your function. Normally WordPress gives you access to that $post object but you need to state that in your metabox callback function like so:
function My_callback_function( $post ){
// your input will be here
}
In the front end you can then simply call get_post_meta again like:
echo get_post_meta($post->ID, 'My-Shortcode', true)
Keep in mind that there are security issues when saving data to and from your WordPress database which is why you would like to use WordPress's nonce system. Here's the link. Investigate to know more: http://codex.wordpress.org/WordPress_Nonces

Categories