Unable to dynamically modify content of a page in WordPress - php

I need to change some opengraph attributes on a specific page based on the query string.
I tried to use some filters based on a installed plugin in the functions.php:
function update_the_og_title($content) {
if(is_page('the-page')) {
// Modify the tags
} else{
// Do nothing
}
}
However, I soon found out the functions.php cannot detect a page using is_page(). What should I do?
How else can I dynamically modify the opengraph tags of a page in WordPress?
Thanks.

You can use is_page but not when functions.php is initially included. What you want to do is to run your code at a specific point in the WP process, using a hook:
function update_the_og_title() {
$id = get_the_id(); //
if(is_page($id)) {
// Get your content here, maybe from metadata?
// Modify the tags
} else{
// Do nothing
}
}
add_action('wp', 'update_the_og_title');
Wordpress action reference
You can get the page ID in a number of ways, including using the $post variable depending on your requirements.

Related

Getting custom field value to functions.php and displaying in on the shortcode

Maybe someone would be able to help me with the issue I have as I'm stuck with no ideas.
I have a shortcode on my site that is responsible for displaying photosets directly from Flickr (via external plugin).
The code generated by the plugin is the following:
[justified_image_grid preset=c1 flickr_user=USERID flickr_photoset=PHOTOSETID]
My blog posts displays various photosets from Flickr. I'd like to avoid having to edit shortcode each and every time to update the shortcode code with the proper photoset ID so I decided to use custom field (Key = FlicktPhotoset, Value = Photoset ID) and add function to functions.php that would create my shortcode which would include original shortcode with the value from custom field.
Code in functions.php is the following:
function flickr_shortcode() {
echo do_shortcode('[justified_image_grid preset=c1 flickr_user=USERNAME flickr_photoset=PHOTOID]');
}
function flickr_shortcodes_init() {
add_shortcode('flickr', 'flickr_shortcode');
}
add_action('init', 'flickr_shortcodes_init');
What I'm stuck at is how to pass shortcode value into this code to automatically fetch PHOTOID from the custom field value.
Something like this:
function flickr_shortcode() {
$FlicktPhotoset = get_post_custom_values("FlicktPhotoset");
echo do_shortcode('[justified_image_grid preset=c1 flickr_user=USERNAME flickr_photoset='.$FlicktPhotoset[0].']');
}
function flickr_shortcodes_init() {
add_shortcode('flickr', 'flickr_shortcode');
}
add_action('init', 'flickr_shortcodes_init');

Creating a shortcode from a backend page

I am trying to create a shortcode from a page that currently resides in the back end. The page has several acf fields as part of a form that creates a request. I would now like to have the same page on the front end. I have tried following the syntax of creating a shortcode from a function after reading about shortocdes, its api and doc and several different tuts online.
add_shortcode('create_requests', array($this, 'load_custom_wp_admin_style'));
^ The attempt above didn't work and I don't get any output when I include the shortcode in a new page.
You can notice that the function I am trying to use 'load_custom_wp_admin_style' returns a null value and uses hooks.
This is the file that contains the function.
Try to include file like below code. I checked your file according to me you need use the plugin url it seems like you are developing the plugin
wp_register_style('your_namespace', plugins_url('style.css',__FILE__ ));
wp_enqueue_style('your_namespace');
wp_register_script( 'your_namespace', plugins_url('your_script.js',__FILE__ ));
wp_enqueue_script('your_namespace');
Assuming that the page you want to display on the front end is a normal WordPress page - created in the pages tab, post type page.
Very simply you can just use the following PHP code to include it in a template:
<?php
$page = get_post(192994);
echo $page->post_content;
?>
If it needs to be a shortcode you can add this into your functions.php:
function output_page_function($atts) {
$page_id = $atts['page_id'];
if (!$page_id) return false;
$page = get_post($page_id);
return $page->post_content;
}
add_shortcode('output_page', 'output_page_function');
And include a shortcode where desired (with 'page_id' attribute)
[output_page page_id=192994]
If it's not a WordPress page, but an actual wp-admin screen, then this would be significantly more difficult/not possible.

If page has posts in certain category - wordpress

I have a client built site that I am adding some functionality too - I don't usually develop using Wordpress. They have built pages using Visual Composer to display posts from varying categories
If the post is within a certain category 'Deals' I want to do stuff … non-working code (in functions.php) below:
function deals () {
if ( in_category('Deals') ) {
echo '<style>.entry-thumb{display: none !important;}</style>';
}
}
Calling function from within child theme page template.
Any help would be great
thanks
You can check if current post is in category by using
if( has_category('Deals') ) {
// do stuff here
}
If $post global variable is set has_category('Deals') will be ok. Otherwise you will need to pass post ID as second parameter. https://developer.wordpress.org/reference/functions/has_category/
P.S. If you are calling it in a loop it looks like you are trying to echo the same inline css multiple times. This will hide all .entry-thumbs regardless of the category. So it may be better to add a class to your deal posts and then use something like .deal .entry-thumb{ display: none; } in your style.css.
You should try is_category() function like this:
function deals () {
if ( is_category('Deals') ) {
echo '<style>.entry-thumb{display: none !important;}</style>';
}
}

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

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)

Categories