How can I modify a Wordpress theme to auto-populate the header image of a Post if one hasn't been set manually? I'm a complete newbie when it comes to PHP.
Using get_post_meta() function you can achieve this functionality. You
can pass post id in first parameter , you can use custom_field_name in
second. And check like below code that it's available in database or
not.
Try this code.
<?php
if(is_single() & !is_home())
{
$myfield = 'custom_field_name'; // Change this to the name of the custom field you use..
$postimage = get_post_meta($post->ID, $myfield, true);
if($postimage)
{
// If the field has a value.. set image path using value...
}
elseif(!$postimage)
{
// If no value than set Default image path..
}
}
?>
Related
Is there method to use wp all export php functions to replace data or maybe any other statement we can use directly inside custom XML export.
What i need is to achieve this logic
I have custom taxonomy in XML called {Conditions} which return value of each post as "Used" "New"
Values appear correctly, but where i need to export XML required different values then those above, which i need to replace during export.
Logic:
If {Conditions} = "Used"
Set Used = "20";
Elseif{Conditions} = "New"
Set New = "10";
WP ALL EXPORT PLUGIN
Please take a look at "Pass Data Through PHP Functions" doc:
"...you can click on the export field and then use the "Export the value returned by a PHP function" option."
https://www.wpallimport.com/documentation/export/pass-data-through-php-functions/
You would specify the function name into the text field as in the screenshot on the documentation page (convert_conditions) or, if running a custom XML export, call the function from the field with something like this:
[convert_conditions({Conditions})]
...and provide the function in the function editor for the field:
function convert_conditions($cond_from_xml = null) {
if($cond_from_xml === 'Used') {
return '20';
} elseif($cond_from_xml === 'New') {
return '10';
} else {
return '0';
}
}
I am using a Toolset plugin for Wordpress and try to make it save the id of the page where the user fills up the form.
This plugin has "hooks" for it.
This is what they say about it.
cred_save_data
Description
This hook allows doing a custom action when post data is saved to the database.
Arguments
post_id. The id of the current created or edited post.
form_data. An associative array of data about current form:
id. The form of ID.
post_type. The post type that the form operates on.
form_type. The type of form - 'new' or 'edit'.
container_id. Refers to the post_id of the post, page or custom post type that contains the CRED form
EXAMPLE:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==12)
{
if (isset($_POST['my_custom_field']))
{
// add it to saved post meta
add_post_meta($post_id, '__my_custom_field', $_POST['my_custom_field'], true);
}
}
}
so I added this modified PHP code in functions.php of the theme:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==2080) // the id of the form
{
if (isset($_POST['container_id']))
{
// add it to saved post meta
add_post_meta($post_id, 'formetadata', $_POST['container_id'], true);
}
}
}
where for metadata is a slug for custom field created to collect metadata, including the id of the page, where the form was filled up.
It does not work. The form collects data, creates a post, but the field for metadata remains empty.
What did I do wrong?
I don't have enough knowledge about PHP and may be completely misunderstood instructions...
So looking at the docs and your function I see they use the key container_id and you use $_POST['container_id']. Im just guessing but it sounds like those are the same id? If so then you can access it via the $form_data like below:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==2080) // the id of the form
{
if (isset($_POST['container_id']))
{
// add it to saved post meta
add_post_meta($post_id, 'formetadata', $form_data['container_id'], true);
}
}
}
I have a dropdown select field to select a custom posttype within the woocommerce product edit screen and need to output the ID of this selected post.
This code works on my frontend but having issues to get it working with other plugins (here wplister).If i hardcode the selected post ID all works fine.
function psg_table_frontend_shortcode($atts, $content = null) {
global $psg_global;
/* START creating HTML Object */
ob_start();
?>
<p>
<?php
echo "The ID of the selected sizing Guide (cpt) inside the Product : " .$psg_global['psg-selected-sizing-guide-on-productpage'];
?>
</p>
$output = ob_get_clean();
/* END creating HTML Object */
return $output;
}
add_shortcode('psg_frontend', 'psg_table_frontend_shortcode');
Spoke to the support of wplister but they said it is probably an issue with the reduxframwork and the global variable.
Any idea?
if you are in a function, who knows if your psg_global is even in scope yet... so try this:
$psg_global= get_option( 'psg_global'); // or whatever your opt name is
$productpage = $psg_global['psg-selected-sizing-guide-on-productpage'];
echo $productpage;
also, might want to use 'psg_selected_sizing_guide_on_productpage' with underscores instead of 'psg-selected-sizing-guide-on-productpage' because that way you can do this:
extract($psg_global)
and all your vars would be in scope.
for example:
echo $psg_selected_sizing_guide_on_productpage
This is from inside my Wordpress plugin, inside the main file:
function my_plugin_install() {
$my_site_url = get_site_url();
$my_options['my_site_url'] = $my_site_url;
// Save
}
register_activation_hook(__FILE__, 'my_plugin_install');
Currently, the install is successful but the 'my_site_url' option is not saved. I'm assuming because the way I'm using the $my_options array at this point doesn't mean anything. It should save this data to the wp_options table.
I can't seem to get this to save, or even find a way to test this as using "echo" gives Wordpress an error during install. Is there a best method for running a script and updating the database during install?
Thanks in advance.
You need to use the WordPress function update_option to save your option value:
function my_plugin_install() {
$my_site_url = get_site_url();
update_option('my_site_url', $my_site_url);
}
register_activation_hook(__FILE__, 'my_plugin_install');
And then later, when you need that value, you can use get_option:
$my_site_url = get_option('my_site_url');
*UPDATE
Since it appears you want to manage multiple of your own options, then I suggest using a simple "utility" function, like so:
function update_my_option($key, $value) {
// Load all of the option values from wp_options
$all_options = get_option('my_options');
// Update just the one option you passed in
$all_options[$key] = $value;
// Save to wp_options
update_option('my_options');
}
And, an appropriate getter function:
function get_my_option($key, $default = NULL) {
// Load all of your options from wp_options
$all_options = get_option('my_options');
// Return just the one option you are asking for
return (isset($all_options[$key])) ? $all_options[$key] : $default;
}
Then, rather than calling update_option directly, you'll call this function, as illustrated below:
function my_plugin_install() {
$my_site_url = get_site_url();
update_my_option('my_site_url', $my_site_url);
}
And, to get one of your options:
$my_site_url = get_my_option('my_site_url');
I added a tinymce text editor into my wp plugin using these codes:
add_action('admin_init', 'editor_admin_init');
add_action('admin_head', 'editor_admin_head');
function editor_admin_init(){
wp_enqueue_script('post');
wp_enqueue_script('word-count');
wp_enqueue_script('editor');
wp_enqueue_script('media-upload');
}
function editor_admin_head(){
wp_tiny_mce();
}
And displaying it:
the_editor("", "content", "", false);
My question is, if i input some things into the editor. Where does it saves the data? On which table?
Depending on how you setup your plugin, this 'could be' saved as an option,
ie:
<?php
// Grab our options, IF your using Options
// if not you can create and use your own tables to store data
$options = get_option('your_plugin_options');
// using a hidden field on the form called action with a value of 'save'
if(isset($_POST['action']) && ($_POST['action']=='save')){
$options['main_content'] = trim($_POST['content']);
$newOptions = array( 'main_content' => $options['main_content'] );
update_option('your_plugin_options', $newOptions );
}
?>
this will create an Option within the wordpress table wp_options
then, if you want to ref that option you simply give it a shout.
<?php
$options = get_option('your_plugin_options');
$new_content = $options['main_content'];
echo $options['main_content'];
//or
echo $new_content;
?>
hopefully this points you in the right direction.
have a read through:
// using the get option
http://codex.wordpress.org/Function_Reference/get_option
// updating options
http://codex.wordpress.org/Function_Reference/update_option
// creating seperate tables in your plugin
http://codex.wordpress.org/Creating_Tables_with_Plugins
Good luck
Marty