Forum Replies Created

Viewing 15 replies - 1 through 15 (of 27 total)
  • Ok an update after further testing:

        public static function get_business_vars($key)
        {
            global $post;
    
            $results = get_post_meta($post->ID, 'custom_group', true);
            return $results[$key];
        }
    $code = get_business_vars('custom_code')
    echo $code;

    Prints “3333”.

                if ( strval($code) !== strval(intval($code)) ) {
                    echo "Your variable is not an integer";
                  } else {
                      echo "yes it is";
                  }

    Prints: “yes it is”

    if ($code == 3333) {
    echo "yes";
    } else {
    echo "no";

    Prints “no”

    I mean…..

    The key is found.

    As explained above, if I echo

    $code = get_business_vars(‘custom_code’) I get 3333.
    $value = get_business_vars(‘custom_value’) I get 5.

    As you mentioned, it seems that they are all stored as strings in the serialised data so it is preventing them being treated as numbers.

    Is there a way to get around this?

    P.s sorry for the inconvenice.

    • This reply was modified 6 months, 2 weeks ago by shaun21uk.

    Hi Joy,

    That code was just an example, it should have been ==.

    So, do I need to do something with the function or with

    $code = get_business_vars('custom_code')

    I tried $code = (int)$code but it changed nothing.

    • This reply was modified 6 months, 2 weeks ago by shaun21uk.
    • This reply was modified 6 months, 2 weeks ago by shaun21uk.

    That’s interesting.

    I actually got it working about 30 minutes ago using add_action.

    I was missing new My_Loyalty_Stamp(); from the bottom of the class, which was a facepalm!

    I will still try your code to see if it works also.

    Thanks man!

    I have also tried changing add_filter to add_action and still no output.

    My custom hook is being used in a page within my plugin.

    Please can you tell me why this won’t work:

    class Report_a_bug
    {
    
        public function __construct()
        {
    
            $this->custom_hook();
            add_filter( 'custom_hook', array( $this, 'report_button' ), 10, 1 );
        }
    
        function custom_hook()
        {
            do_action('custom_hook');
        }
    
        public function report_button($content)
        {
            $content .= '<div class="report-a-bug">
            <button class="show-form" data-post_id="' . get_the_ID() . '">' .
                __('Report a bug', 'reportabug') .
                '</button>
            <textarea class="report-a-bug-message" placeholder="' . __('Describe what\'s wrong...', 'reportabug') . '"></textarea>
            <p class="report-a-bug-response"></p>
        </div>';
    
            return $content;
        }
    }

    Nothing shows when I call new Report_a_bug();

    I know the class is registered because I used a simple hello world output and that output fine.

    Thanks bcworkz,

    I think an ajax solution is worth researching as it sounds like it will do the job with the added security I am looking for.

    Good explanation though, appreciated πŸ™‚

    Hi bcworks,

    I will try and answer as best I can.

    1. The input form is only visible for a logged in user. When they submit the form to the custom.js file, it grabs their input and sends it on to the api for validation.

    In the api endpoint/validation – I pull a code from the database and check it against the $params[userInput].

    If the code matches then the endpoint sends (within the return data) a success message. Otherwise it sends a failure.

    The reason I wanted to add a nonce (or some form of security) is I was concerned about the database code being intercepted.

    Is it ok as it is or would you wrap more security around this process?

    2. v1 vs v2 – this is my first time creating an api, it worked so I didn’t “fix it”. Do I need to change this?

    I mean, am I going about this in all the wrong way?

    All I am trying to do is take the code entered in the form and securely check that code against a code stored in the database.

    All without moving off the page. Currently as you can see, I am using a custom api endpoint to achieve this.

    Ah that’s something I had not considered.

    So table 1 would house:

    userid, custom1, 2, 3, 4, 5, 6

    table 2 would house:

    userid, date, transaction

    Thanks that is definitely worth considering.

    Hi Bcworkz,

    Thanks, it wasn’t the code I was hooked on, it was the question.

    How would I modify such a table-creation code to allow colum 7 (history) to accept an array which can be added to with two values: date and transaction?

    The code was just an example to play with.

    Thanks for taking the time to respond. As with most things, a leg up and a lot of reading will usually solve the problem πŸ™‚

    shaun21uk

    (@shaun21uk)

    Simply changing JQuery to jQuery made it all come alive!

    Thank you so much for your fresh pair of eyes.

    Now to look into your comment around:

    “Don’t put the logic into the client side. Write that logic in PHP and only put as few details as needed into the page for JS to use. The JS should authenticate with the server before making any adds or updates. Use nonces to make sure the user is who they said they were, and is only accessing the thing that was loaded into the page.”

    ….I may be back, but thank you!

    shaun21uk

    (@shaun21uk)

    Thanks for responding Joy.

    This is what I have come up with so far in the meantime:

    child functions.php:

    add_action('wp_head', function () {
    // Register the script
    wp_register_script( 'custom_script', get_stylesheet_directory_uri() . '/custom.js' );
    
    // Localize the script with new data
    $custom_api_vars = array(
        'pid' => get_the_id(),
        'uid' => get_current_user()
    );
    wp_localize_script( 'custom_script', 'custom_vars', $custom_api_vars );
    
    // Enqueued script with localized data.
    wp_enqueue_script( 'custom_script' , array('jquery'), '1.0', true);
    
    });

    custom.js:

    function redeem() {
        const pidR = custom_vars.pid;
        const useridR = custom_vars.uid;
        fetch(<code>http://....../wp-json/api/v1/customquery?code=${userInput}&pid=${pidR}&userid=${useridR}</code>).then(r => r.json()).then(data => {
            .....do stuff.....
        })
    };

    Unfortunately I am getting this error in console:

    Uncaught (in promise) ReferenceError: JQuery is not defined

    Am I on the right track?

    I am confused how to perform the add/update_user_meta within my php as I cannot get the result back into php, which is why it is included within the function.

    html/php -> collect inputcode from form
    js -> pull the correct code from the database for that post and check against inputcode. If correct then add/update user meta.

    All this happens in AJAX. Without getting the result back into php, how can I perform the add/update user meta within my php?

    I have never looked into nonce but will do so now.

    Fair point Dion, thanks for your reply.

    So, look into a new table to house my plugin data?

    Column headers: userid, postid, custom1, custom2, custom3 etc?

    In this scenario, would custom6 (from question2) still be stored as an array in the custom6 column?

Viewing 15 replies - 1 through 15 (of 27 total)