I want to pull data from my event calender. So I added REST API support to my event content type. But my JSON output is with HTML.
Can I not get the raw database data?
I get this: http://astc.dk/wp-json/wp/v2/event/
I put this in my functions.php
add_action( 'init', 'my_event_cpt', 25 );
function my_event_cpt() {
global $wp_post_types;
//be sure to set this to the name of your post type!
$post_type_name = 'event';
if( isset( $wp_post_types[ $post_type_name ] ) ) {
$wp_post_types[$post_type_name]->show_in_rest = true;
// Optionally customize the rest_base or controller class
$wp_post_types[$post_type_name]->rest_base = $post_type_name;
$wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
}
}
Related
I have to update my product with data that comes from an API query. I would like to update the products data when visiting the single products view. I'm new to WordPress and WooCommerce, but I think this can be done through hooks?
I've tried looking for hooks but haven't had any luck yet
You could do something like the example below.
This will run every time the product is viewed. Of course, you have to add your own API call and adapt the function to your data structure.
add_action('wp', 'update_product_data');
function update_product_data(){
if (class_exists('WooCommerce') && is_product()) {
$product = wc_get_product( get_the_ID() );
$product_id = $product->get_id();
// Call the external API to get the updated product data
$api_url = 'http://example.com/api/product/' . $product_id;
$response = wp_remote_get($api_url);
// Check for successful response and decode the JSON data
if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) == 200) {
$data = json_decode(wp_remote_retrieve_body($response), true);
// Update the product's weight and description with the data from the API
$product->set_weight($data['weight']);
$product->set_description($data['description']);
$product->save();
}
}
}
Using Advanced Custom Fields, I've added a "Post Object" field to my Woocommerce edit page, set to return Post IDs.
I'm trying to get data from these posts, and automatically populate my Woocommerce Downloadable Files from them.
The problem with my current code is that if I change the data in the custom field, and then save my product, it doesn't update the downloads. I need to update the product again for the downloads to be affected.
add_action( 'woocommerce_admin_process_product_object', 'mysite_add_downloads', 50, 1 );
function mysite_add_downloads( $product ){
$mysite_select_albums = get_field('mysite_select_albums');
$downloads = array(); // Clear $downloads variable
foreach ($mysite_select_albums as $album) {
// Gets Information About Download From Album Post Type
$post = get_post($album);
$file_title = $post->post_title;
$file_url = "http://changingthinstosimplify.com/";
$file_md5 = md5($file_url);
$download = new WC_Product_Download(); // Get an instance of the WC_Product_Download Object
// Set the download data
$download->set_name($file_title);
$download->set_id($file_md5);
$download->set_file($file_url);
$downloads[$file_md5] = $download; // Insert the new download to the array of downloads
}
$product->set_downloads($downloads); // Set new array of downloads
}
I am trying to set the post custom taxonomy via two variables (parent Id and child Id) that I've gotten from another function in my plugin file. var's $a and $b
When I manually enter the term ID - everything works fine but when I try to use a variable - to set the term id - it doesn't work
The function for getting the id's in the first code snippet, and trying to save the terms in the second snippet.
//***IF YOU HAVE MODEL GET THE ID
if(!empty($auto_model)){
$auto_model_id = get_term_by( 'slug', $auto_model, $this->tax );
$auto_model_id = $auto_model_id->term_id;
} // end get model id
if ($auto_make_id !== null && $auto_model_id !== null) { // +++++ IF YOU HAVE BOTH
$this->p_id = $auto_make_id; // define variable
$this->c_id = $auto_model_id; // define variable
// set post with both these terms - jump to set post and pass ids to set post
//return $p_id;
return $c_id . $p_id;
}
function cicb_do_things($post_id) { // use this as the final build up before sending to construct
global $auto_make,$auto_model;
$this->makemodel();
$a = $this->p_id;
$b = $this->c_id;
$tag = array( 45 ); // this is how wp wants me to do it; and when I do the wp_set object works great
var_dump ($tag);
echo '<br>';
$tag = array( $a ); // when I put in my variable which is the also 45 - the term does not work
wp_set_object_terms( $post_id, $tag, $this->tax );
}
I am using the gravity form on my site. I am working on create the custom report for this I have need gravity form fields name and id based on specific form id.Please let me know how I can do it.
I am using the following function but it is showing all forms info based on it. it is looking very hard to parse it. Please let me know any function so I can get fields name easily.
$forms = RGFormsModel::get_forms_by_id(13);
try this
function get_all_form_fields($form_id){
$form = RGFormsModel::get_form_meta($form_id);
$fields = array();
if(is_array($form["fields"])){
foreach($form["fields"] as $field){
if(isset($field["inputs"]) && is_array($field["inputs"])){
foreach($field["inputs"] as $input)
$fields[] = array($input["id"], GFCommon::get_label($field, $input["id"]));
}
else if(!rgar($field, 'displayOnly')){
$fields[] = array($field["id"], GFCommon::get_label($field));
}
}
}
//echo "<pre>";
//print_r($fields);
//echo "</pre>";
return $fields;
}
It's not that hard to parse:
$fields=array();
foreach ( $forms['fields'] as $field ) {
$fields[]=array($field['id'] => $field['inputName']);
}
P.S. I'm assuming you use Gravity Forms < 1.7 as RGFormsModel::get_forms_by_id is a deprecated function since 1.7
// Get the Form fields
$form = RGFormsModel::get_form_meta($form_id);
// Run through the fields to grab an object of the desired field
$field = RGFormsModel::get_field( $form, $field_id );
I use the above to get a specific field I want to filter the value of. The $field contains an object with all the properties you want.
echo $field->label // Gets the label
echo $field->inputName // Gets the name
echo $field->type // Gets the type
echo $field->cssClass // Gets the CSS Classes as a string
You are able to get the entered value/content of a field by using rgpost() and by referencing the id ($field->id).
// Check the entered value of every field
foreach( $form['fields'] as &$field ) {
// Get the content for the specific field
$fieldContent = rgpost( "input_".$field->id );
// Check the content
if ( $fieldContent == ... ){}
}
i modify content when saving and need to access post title, what i have so far in functions.php
add_filter('content_save_pre', 'custom_content_save_pre');
function custom_content_save_pre($s) {
// need to access post title here
$postTitle = ?
// some more code here
return $s;
}
Globalize the $post object and you will have access to it ex:
add_filter('content_save_pre', 'custom_content_save_pre');
function custom_content_save_pre($s) {
global $post;
$postTitle = $post->title;
return $s;
}
but a better hook to use would be wp_insert_post_data which accepts 2 parameters $data and $postarr
where data is an array of the post object that is needed to be returned for saving.