skip to Main Content

I’ve given up on programing about 17 years ago, in the times before proper internet in my country. Now I can’t afford a programer so i’ve taken up the task myself, yet my limited coding knowledge seems to have gotten me quite stuck..

Situation:

in wordpress, i have the folowing keys stored in the postmeta table for each post_id: awb_urgent_cargus, _fgo_invoice_link

Problem:

I need to create a shortcode that should display 2 input fields: [$post_id] and [$awb]. If the user enters a matching pair he gets the corresponding invoice link from the same $post_id.

Basicly after submiting,

if the corresponding awb_urgent_cargus key for the input [$post_id] is the value of input [$awb] then return the value for the key _fgo_invoice_link

else, return error

I found some documentation for this but i got stuck writing the code … couldn’t even get the inputs working 🙁
I think that swiching from ms-dos coding in c++ / pascal to the web is quite a bit over my head and while i managed to get around and modify examples for other stuff this one just simpli doesn’t click

get_post_meta( int $post_id, string $key = ”, bool $single = false );

2

Answers


  1. You could handle input with ajax, wordpress requires you to register allowed actions on backend, perform your check and return the result.

    //js
    var value = jQuery.ajax({
                    type: 'POST', 
                    url: ajaxurl,
                    cache: false,
                    data:{
                    nonce : nonce,
                    action: 'FUNCTIONNAME',
                    },
                    success: function(data, textStatus, jqXHR) {
                        console.log(data.status);
                    },
                    error: function(jqXHR, textStatus, errorThrown){    
                        console.log('ajax error');
                    }
                })
    
    };
    

        //php    
        add_action('wp_ajax_FUNCTIONNAME', 'FUNCTIONNAME');
                    
        function FUNCTIONNAME() {
        //your backend logic here
    $data = array();
    $data['status'] = 'all good';
    echo json_encode($data);
    die();
        }
    
    Login or Signup to reply.
  2. You can put it all together like this.

    function show_post_meta_value( $args ){
    
        $atts = shortcode_atts( array(
            'post_id' => '',
        ), $atts, 'show_post_meta_value' );
    
        if( $post_id != '' ){
            $awb_urgent_cargus = get_post_meta( $post_id, 'awb_urgent_cargus', true );
            $_fgo_invoice_link = get_post_meta( $post_id, '_fgo_invoice_link', true );
    
            return 'awb_urgent_cargus ==> '. $awb_urgent_cargus.' and _fgo_invoice_link ==> '.$_fgo_invoice_link;
        }
    
        return '';
    
    }
    add_shortcode( 'show_post_meta_value', 'show_post_meta_value' );
    

    and use like this [show_post_meta_value post_id="yourpostidhere"]

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search