Add woocommerce filter after ajax function [closed]

Below is my function which runs on ajax and it upload image in media, now i want to add this in my woocomerrce cart but it does not work.

here $cart_item_meta returns path of the file in array.

function my_action_callback(  ){

            $addon_id = array();

            if( isset($_FILES) ){

$attach_info=wp_upload_bits($_FILES["file"]["name"], null, file_get_contents($_FILES["file"]["tmp_name"]));

$attach_id = wp_insert_attachment($data, $attach_info['file'], 0);

                $addon_id['media_id'] = $attach_id;
                $addon_id['media_url'] = wp_get_attachment_url( esc_attr( $attach_id ) );
                $cart_item_meta['wau_addon_ids'][] = $addon_id;

            add_filter( 'woocommerce_add_cart_item_data', $cart_item_meta, 10, 2 );


Go to Source
Author: Vishal

WordPress Settings API using angularJS

i’m trying to update WordPress General Settings page with AngularJS with a $

$scope.update = function(genSet){
       $scope.generalSettings = angular.copy(genSet);
       $'options-general.php', JSON.stringify($scope.generalSettings)).then(function (response) {
       }, function (response) {                 

The form itself is composed of two input fields with the same name as those in options-general.php

The form posts and the response is successful, though the settings page itself doesn’t change.

Am i missing something here?

Go to Source
Author: Alexandru Andrei

Doing a validation check on a AJAX post and returning the error message

I have an AJAX post that does this

                    type: "POST",
                    url: "@MyWebSite.Url/myController/myView",
                    contentType: "application/json; charset=utf-8",
                    JSON.stringify({ myModel: myData }),
                    dataType: "json",
                    traditional: true,
                    success: function () {
                    error: function () {
                        alert('Error! ');

My controller does the validation check but it is not correctly returning the error message.
This is what my controller looks like

 if (totalQty < part.QtyInItem)
                        //ModelState.AddModelError("", "There is " + part.QtyInItem + " of Part " + part.PartName + " used in item " + part.ItemID + " but you only handled " + totalQty + ". Please verify you are handling all parts used in the item.");
                        //RedirectToAction("myControler", myModel);
                        return this.Json(new { success = false, message = "There is " + part.QtyInItem + " of Part " + part.PartName + " used in item " + part.ItemID + " but you only handled " + totalQty + ". Please verify you are handling all parts used in the item." });

When I tried adding an error to the model state it just returned “ERROR!” and not the error message I had associated with it. And when I try doing the this.JSON return it returns “success” to the view and not the error message.

How can I do this validation check for my AJAX post

Go to Source
Author: ryan

Problem when sending file via ajax

I am trying to send a file via ajax but I get a 400 bad request error.
My code

data.coverQuestions.medicalMalpractice.file = docFile.prop('files')[0];

url: '/wp-admin/admin-ajax.php',
method: 'post',
data: {
  action: 'insurance_form_data',
contentType: false,
processData: false,
success (res){

If remove the parameters and do not send files, then in this case it is successfully sent.

contentType: false processData: false

What could be the problem?

Go to Source
Author: Pavel

How to load new text widgets and polls into the sidebar without reloading the page?

We are creating a WordPress website page for the online broadcast of the conference. The page should work so that when a site visitor opens this page, the content on this page is automatically loaded without reloading the page itself.

In the right column there will be this chat “Wise Chat Pro”.
In the central column there will be a video player to watch the broadcast from YouTube, and under it will be a block where you can ask the speaker of the conference a question and the questions themselves will automatically appear without reloading the page, as in this example: https: //themes.webberwebber. com / live-neutral / live-relation-directly-from-an-imporant-event /. How to implement this is understandable, using the comments on the page and automatically load them by ajax

The problem is that in the left column of the site, as well as at the bottom of the central column, when a new quote or survey is published, they are automatically loaded on the page, without reloading the page itself.

Is it possible to load new quotes and polls into the sidebar of the WordPress site without reloading the page? I would be very grateful for a hint in which direction to move and how this can be implemented.

enter image description here

Go to Source
Author: Danil Pridvorov

How do I mitigate replay attacks when talking about actions that shouldn’t happen twice?

A nonce is supposed to first help me against CSRF and help against replay attacks is just a bonus if I “personalize” the nonce to something like pay-user-{id}, but here’s the problem – if my link looked like /wordpress/admin_ajax.php?action=pay-user&id=20&security=ej3548 I have 2 cases to take care of:

  1. I created a nonce without the specific user ID, pay-user – if an attacker obtains the nonce, he can make me click that link and pay any user.
  2. I created a nonce with the specific user ID, pay-user-{id} – if an attacker obtains the nonce, he can only make me replay that request, since the nonce was made to verify that specific (to {id}) action.

But that’s still an issue in a lot of cases, paying someone is a prime example. I can’t be made to pay someone else, but if I make that request 10 times, I’ll pay that specific person 10 times.

Is there no specific “per request” hashing?

What to do?

Go to Source
Author: Daniel Simmons

What is the correct way to call a PHP Function with multiple parameters from the AJAX Handler?

Q: What is the correct way to call a PHP Function with multiple parameters from the AJAX Handler?

Q: Am I calling the php function correctly?

Page Element
The user clicks a button.

The click should connect to an external server using the following params:

  1. User Name 2. IP Address 3. Port 4. Public Key 5. Service

Send an vote string (from host A) to an external Gaming server (host B).
I do not need to update any wordpress content.

WordPress Version: 5.4.1
I put my custom php code into “/wp-contents/plugins/my-plugin/votifier.php”

I made sure the custom plugin is activated.

I have WordPress in Debug mode. i.e. Debug mode is true.

The Button

<div id="frm_field_61_container">
<button type="button">Try it</button>

WordPress JQuery with AJAX

jQuery(document).ready( function($) {


         type : "post",

         dataType : "json",

         url : myAjax.ajaxurl,

         data : {    action: "my_vote_count"
                    ,username: $('input[name="item_meta[59]"]').val()
                    ,key: $.trim($("#field_yjr62").val())
                    ,ip: $('input[name="item_meta[40]"]').val()
                    ,port: $('input[name="item_meta[42]"]').val()
                    ,service: "Votifier"

        success:    function(data,status,xhr){
                    alert("Data: " + data);
                    alert("Status: " + status);
                    alert("xhr: " + xhr);

My Custom PHP Script


function mainVotifier($username, $public_key, $server_ip, $port, $service_name) {

define( 'VOTE_FORMAT', 'VOTEn%sn%sn%sn%dn' );
define( 'PUBLIC_KEY_FORMAT', '-----BEGIN PUBLIC KEY-----n%sn-----END PUBLIC KEY-----' );

$key    = wordwrap($public_key, 65, "n", true);
$key    = sprintf(PUBLIC_KEY_FORMAT, $key);

  if (php_sapi_name() !== 'cli') {
            //Detect proxy and use correct IP.
            $address = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
        } else {
            //Script is run via CLI, use server name.
            $address = $_SERVER['SERVER_NAME'];

    $data = sprintf(VOTE_FORMAT, $service_name, $username, $address, time());
    openssl_public_encrypt($data, $crypted, $key);
    $socket = @fsockopen($server_ip, $port);

    if ($socket) {
        if (fwrite($socket, $crypted)) {
            return true;



/* ===== WP_ENQUEUE SCRIPTS ==== */

/* ===== JQuery Ajax Script to call votifier.php ==== */
add_action( 'wp_enqueue_scripts', 'my_script_enqueuer' );
function my_script_enqueuer() {

   wp_enqueue_script( 'my_voter_script',  plugins_url('sendvote.js', __FILE__), array( 'jquery'),   '1.0', true);

   wp_localize_script( 'my_voter_script', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));


/* ===== My AJAX Handler ==== */

/* add_action( 'wp_ajax_nopriv_my_vote_count', 'my_ajax_handler' ); */ 
add_action( 'wp_ajax_my_vote_count', 'my_ajax_handler');
function my_ajax_handler() {

/*  check_ajax_referer('votifier_response_key'); */

    $username       = $_POST["username"];
    $public_key     = $_POST['key'];
    $server_ip      = $_POST["ip"];
    $port           = $_POST["port"];   
    $service_name   = $_POST["service"];

   echo "mainVotifier("" . $username . "", "" . $public_key . "", "" . $server_ip . "", "" . $port . "", "" . $service_name . "");";

    wp_die(); // All ajax handlers die when finished


Console Results

The console results are good, but the other server is not responding.
I tested the PHP code locally from my personal computer and Server B is responding.

Request URL:
Request Method:POST
Remote Address:
Status Code:

Go to Source
Author: ScottUSA

access the data from ajax response.!

access the data from ajax response.!

Is there any body i need little help plz. I’m trying to access ‘id’ from ajax response that is coming from database (in row_array()) but when when i alert it or console it i only get single bracket ( { ). Thanks!

                    type : 'post',
                    url : '<?php echo site_url() ?>main_controller/verify_email_for_password_reset',
                    data : {
                        verify : verify
                    success : function(data)
                        var html = '<button type="button" id="change_password" data-id = "'+data+'" class="btn btn-block btn-hero btn-noborder btn-rounded btn-alt-success"><i class="si si-user-follow mr-10"></i> Change</button>';

                var id = $(this).data('id');

Go to Source
Author: Abdul Basit