Add ACF to custom function - php

I need to add my ACF field to a custom function. I have tried this but there is syntax error.
add_action( 'flatsome_custom_single_product_1', function () {
echo <?php the_field('designer'); ?>;
} );

When you are calling the_field('designer') outside the actual post loop, you need to pass the post ID as well.
Here is the Ref.
For your case, you can achieve this by using the code like below
add_action( 'flatsome_custom_single_product_1', function () {
global $post;
echo get_field('designer', $post->ID);
} );

Related

What's the shortcut needed for this PHP code

I have this PHP script:
function myshortcode_title( ){
return get_the_title();
}
add_shortcode( 'page_title', 'myshortcode_title' );
What is the shortcut i need for the page title to be shown? What do i need to put in the HTML for the fetched title to be shown
Add this to your theme, or make an plugin from it.
/* title to get the post title */
function getPageTitle() {
global $wp_query;
return get_post_title($wp_query->post->ID);
}
/* Add shortcode */
add_shortcode('page_title', 'getPageTitle');
for more ShortCode Api
You may use this.
echo do_shortcode('[page_title]');
https://developer.wordpress.org/reference/functions/do_shortcode/

Can't pass value from one function to another - WordPress ACF

I want to check if select field changed it's value after post editing, and if it is, then to send an email to admin.
I saved previous value to a variable $pre_status_eksperimenta using acf/pre_save_post, like this:
function action_pre_post_update( $post_id ) {
$pre_status_eksperimenta = get_post_meta($post_id, 'status', true);
};
add_action( 'acf/pre_save_post', 'action_pre_post_update', 10, 1 );
When I var_dump($pre_status_eksperimenta) I get correct value, wich means it works.
Then I want to pass that to a acf/save_post hook and check if there was a change, but now when I var_dump($pre_status_eksperimenta) I get NULL
function status_change_notification($ID) {
var_dump($pre_status_eksperimenta);
die();
}
add_action( 'acf/save_post', 'status_change_notification', 10, 1);
I think its about variable scope. You should global it when you use it in another function.
function action_pre_post_update( $post_id ) {
global $pre_status_eksperimenta;
$pre_status_eksperimenta = get_post_meta($post_id, 'status', true);
};
and then
function status_change_notification($ID) {
global $pre_status_eksperimenta;
var_dump($pre_status_eksperimenta);
die();
}

How to Fire an Event Only Once on WordPress Post Publish?

Thanks for any help with this.
I need to know how to hook wp_insert_post (or whatever is similar and better?) without it firing multiple times. What is the correct way to do this in WordPress?
For example:
In Plugin.php
add_filter( 'wp_insert_post', 'add_data') );
...
function add_data()
{
// This line is outputted twice
terseErrorLog("This code was executed.");
}
Try this:
function add_data() {
global $post;
if ($post - > post_status == "publish") {
terseErrorLog("This code was executed.");
}
}
add_action('save_post', 'add_data');

How to add values to a Wordpress template file programmatically?

I'm new to Wordpress & PHP, so kindly excuse the naivety of the question, if any.
I'm building a plugin where I need to select values from the database and create a new HTML page with the values. I'm using a custom template file.
What I've done till now
Extracted the values from database
Load & display my own template in my plugin file
add_action( 'init', 'leb_add_endpoint' );
function leb_add_endpoint()
{
add_rewrite_endpoint( 'result', EP_PERMALINK );
}
add_action( 'template_include', 'leb_render_template' );
function leb_render_template($default_template) {
//some code removed for brevity
if ( ! is_singular() || !isset($wp_query->query_vars['result']) )
{
return $default_template;
}
$sample_result = $wpdb->get_var($wpdb->prepare($sql));
$default_template = plugin_dir_path(__FILE__) . '/my-custom-template.php';
return $default_template;
}
The content of my-custom-template.php is as follows
<?php
/* Template Name: My Template*/
echo '<h1>Testing</h1>';
?>
The page gets displayed without any problem. All I want is to insert $sample_result and other similar results pulled form database into my-custom-template.php
I need to generate dynamic pages based on values pulled from DB. So each time, the final page created might be different. E.g. if one hits www.example.com/sample-post/result, a page will be shown with values pulled from the DB. If one hits www.example.com/another-sample-post/result, a different page will be shown with different values. Both these pages will have the same design, only a few values will be different. This is what I'm trying to achieve.
How can I do that? Please help me. I'm stuck. :(
You need to define "result" in query vars.
Use EP_ROOT Endpoint Mask ( result/{var} is located in the root )
Inside template_include hook, you can find result value inside $wp_query object.
I've already tested the code
// Add a new var to query vars
function result_add_query_vars( $vars ){
$vars[] = 'result';
return $vars;
}
add_filter( 'query_vars', 'result_add_query_vars' );
// Add endpoint
function result_add_endpoint() {
add_rewrite_endpoint( 'result', EP_ROOT );
}
add_action( 'init', 'result_add_endpoint');
// change the template
function result_render_template($template)
{
global $wp_query;
if ( array_key_exists( 'result', $wp_query->query_vars ) ) {
$result = get_query_var('result');
$new_template = plugin_dir_path(__FILE__) . '/my-custom-template.php';
return $new_template;
} else {
return $template;
}
}
add_action( 'template_include', 'result_render_template' );
Now you can retrieve the query var in your custom template
/*
* Template Name: My Custom Template
*/
$result = get_query_var('result');
echo $result;
Well why don't you use $wp_query Inside your my-custom-template.php
<?php
/* Template Name: My Template*/
global $wp_query;
echo '<pre>';
print_r($wp_query); // Use this in case you want to see what else do you have with you.
echo '<pre/>';
// Now you can use $wp_query to build your dynamic query at run time.
// This will allow you to perform task at run time
?>
To Retrieve Meta
If you have saved something as a meta then
<?php
$meta_values = get_post_meta( $post_id, $key, $single );
?>
To Retrieve Child Post
If you want to retrieve child posts then
<?php
if ( have_posts() ) :
// Start the Loop.
while ( have_posts() ) : the_post();
// Do your stuff here such as below
the_title();
the_content();
endwhile;
else:
echo 'No Post Found';
endif;
?>
1) Write this function in function.php in your active template.
function leb_render_template() {
//some code removed for brevity
//$sql = 'YOUR_QUERY_CODE'
$sample_result = $wpdb->get_var($wpdb->prepare($sql));
return $my_template;
}
add_action('wp_ajax_leb_render_template', 'leb_render_template');
add_action('wp_ajax_nopriv_leb_render_template', 'leb_render_template');
2) Call function in your custom template.
<?php
/* Template Name: My Template*/
echo '<h1>Testing</h1>';
$result = leb_render_template();
print_r($result); // Print Your function output.
?>

Wordpress append to HEAD from within shortcode callback

I am making a plugin, to register a shortcode, which when used like this: [append_css mycss] it will look for a custom field called mycss and add the contents to the head of the document. It all works great, except that the code is being added to the body, and I don't know how to get it to add to the head.
I have tried adding an action wp_head but I don't know how to pass variables whilst doing that, and it does not seem to fire from within the shortcode callback anyway.
function append_css_short($params){
global $post;
if(sizeof($params)){
$key = $params[0];
} else {
$key = 'css';
}
return '<style type="text/css">'.
get_post_meta($post->ID, $key, true)
.'</style>';
}
add_shortcode('append_css','append_css_short');
How would I get this to write to the head instead of body?
Is there a better approach to the problem?
If you want to print something to the head of the document you need use add_action with wp_head hook.
A better approach might be to not use a shortcode at all. Shortcodes are intended to add content or modify content. Instead create a function that you can attach to wp_head that will print your css. You may want to force the user to keep the custom field name consistent. Or better yet have your plugin add a metabox to posts that they can enter custom css into. Then you will always be certain of the name of the field.
function append_css() {
global $post;
if ( ! get_post_meta( $post->ID, 'mypluginname_css', true ) ) {
return;
}
return '<style type="text/css">'. get_post_meta($post->ID, $key, true) .'</style>';
}
add_action( 'wp_head', 'append_css' );
The only thing i am not certain of is whether or not $post with the custom field data will be available outside the loop. So you may have to add some extra querying in there to pull the custom data at the time that wp_head is fired.
With some minor adjustments to the answer provided by #Jrod, here is the complete working plugin, with usage as follows:
Create custom field called append_css (or append_js) with required code
... of course, there is no number 2 ;)
Code
<?php
/**
* #package Append_CSS_JS
* #version 0.1
*/
/*
Plugin Name: Append CSS JS
Plugin URI: http://itaccess.org/wordpress-plugins/append-css-js/
Description: Append Css or Js to any page using Custom Fields
Author: Billy Moon
Version: 0.1
Author URI: http://billy.itaccess.org/
*/
function append_css() {
global $post;
if ( ! get_post_meta( $post->ID, 'append_css', true ) ){
return;
}
echo '<style type="text/css">'. get_post_meta($post->ID, 'append_css', true) .'</style>';
}
add_action( 'wp_head', 'append_css' );
function append_js() {
global $post;
if ( ! get_post_meta( $post->ID, 'append_js', true ) ) {
return;
}
echo '<script type="text/javascript">'. get_post_meta($post->ID, 'append_js', true) .'</script>';
}
add_action( 'wp_head', 'append_js' );
?>
I don't think this is possible that way, because shortcodes will be called after wp_head(), so you cannot hook on it.
Here's the list of all wordpress hooks http://adambrown.info/p/wp_hooks
EDIT:
Here's a workaround:
In your template:
<?php
//get the content of your post:
if(have_posts()) while(have_posts()) the_post();
$pcontent = get_the_content();
endwhile;
?>
<html>
<head>
...
</head>
<body>
...
<?php echo $pcontent; ?>
</body>
</html>

Categories