• Hi Team,

    Is it possible to pass the custom field data to API without storing it in the database?

    • This topic was modified 3 years, 10 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator threadi

    (@threadi)

    If you create a custom endpoint, you can call a function via callback that will output any data you want: https://developer.ww.wp.xz.cn/rest-api/extending-the-rest-api/adding-custom-endpoints/

    If you are interested in an API endpoint, have a look here https://developer.ww.wp.xz.cn/rest-api/extending-the-rest-api/routes-and-endpoints/ under “Endpoints”. There is an example of one with static data in the return.

    Thread Starter monishakrishnan

    (@monishakrishnan)

    Hi @threadi

    Thanks for your response.
    Can I ask you one more doubt?

    I have created a custom plugin with 3 to 4 fields.
    and I have created a new database as well.

    Now, how to get the values from custom fields and store them in my custom table?

    Moderator threadi

    (@threadi)

    Use wbdb’s insert-method: https://developer.ww.wp.xz.cn/reference/classes/wpdb/insert/ – with this you can add records to your table. You would only have to define yourself at which moment this should happen. When the plugin is activated, at the push of a button by a user, at the first call of a page after the plugin has been activated … everything is up to you and there are hooks for everything.

    Thread Starter monishakrishnan

    (@monishakrishnan)

    HI @threadi

    I tried like this.. Is this correct?
    It is not updating for me.

    add_action(‘acf/save_post’, ‘my_acf_save_post’,1);

    function my_acf_save_post( $post_id ) {

    global $wpdb;
    $table_name = $wpdb->prefix . ‘ngp_labels’;

    $wpdb->insert( $wpdb->myTabel, array(
    ‘id’ => $post_id,
    ‘name’ => $fields[‘field_gf7623rgh’],
    ‘address’ => $fields[‘field_62ferfef’],
    ‘label_author’ => $user_id,
    ‘updated_date’ => $now
    ));

    Moderator threadi

    (@threadi)

    What is $wpdb->myTabel? There should be $table_name I think.

    Apart from that, a “}” is missing at the end (maybe just a copying error).

    You can also test possible SQL errors per:

    $wpdb->show_errors();
    $wpdb->hide_errors();

    Or use https://ww.wp.xz.cn/plugins/query-monitor/ to see if and what exactly is executed. The plugin would be my recommendation anyway for anyone doing database operations with their plugin.

    Thread Starter monishakrishnan

    (@monishakrishnan)

    Hi @threadi

    I have tried like this..but i am getting an error only.. i am not able to click add new , it is throwing the error statement. Can you please help on this?

    add_action( 'save_post', 'save_post_to_other_table', 10, 3 );
    function save_post_to_other_table( $post_id ) {
    
        global $wpdb;
    
        $name=$_POST['name'];
        $address=$_POST['address'];
    
        if($wpdb->insert(
            'table_name',
            array(
                    'name' => $name,
                    'labelName' => $address,
                    
                )
    ) == false) wp_die('Database Insertion failed'); else echo 'Database insertion successful<p />';
    }
Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Custom field data to API’ is closed to new replies.