• Resolved ace0930

    (@ace0930)


    Hi, is it possible to get the custom field value by only JavaScript because I don’t know PHP.

    Let’s say I got a post type ‘test’ and there is one custom field attached to it ‘name’.

    If there is a button with the class name btn on the frontend, how do I get the value of ‘name’ by clicking the button? Something like this:

    
    const btn = document.querySelector('.btn');
    
    btn.addEventListener('click', function() {
       let fieldValue;
       console.log(fieldValue);
    })
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Anh Tran

    (@rilwis)

    Hi @ace0930,

    If you want to get custom field value when editing a post, then you can inspect the input to get its value, like this:

    const fieldValue = document.querySelector( '#fieldId' ).value;

    However, it’s very limited and can’t be used on the front end or elsewhere (not edit post screen). WordPress and the plugin are PHP apps, so PHP is the standard way to get field value.

    Thread Starter ace0930

    (@ace0930)

    Can you give an example of getting the custom field value by PHP way? So I know how to do this in the future when I learn PHP…My use case will be clicking the button on the frontend to get the custom field value.

    Plugin Author Anh Tran

    (@rilwis)

    Here is this docs for displaying fields with code. Please take a look.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get Custom Field Value’ is closed to new replies.