Skip to main content
edited body
Source Link
kaiser
  • 50.8k
  • 27
  • 150
  • 244

You need to have some initial (maybe global) js var markers_array = [];. Then use an ajax callback function to bump your markers into that array.

Register your scripts

// Define $root_url = plugin_dir_path(); // Example path $ajax_nonce = 'your_nonce'; $ajax_nonce_val = 'your_val'; // Ajax wp_register_script( 'json2', false, false, false, true ); wp_enqueue_script( 'json2' ); wp_enqueue_script( 'ajax-script' ,"{$root_url}js/ajax_json.js" // your ajax callback script that handles the request (inside a /js folder) ,array( 'json2' ) ,false ,true ); wp_localize_script( 'ajax-script' ,'ajax_object' // YOUR MAIN DATA OBJECT ,array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ,$ajax_nonce => wp_create_nonce( $ajax_nonce_val ) ) );

The initial Markers

// inside some part: You need the following php function to build your markers array: // This js array will be used by your custom build markers js function to build the initial markers. It can also be used to update markers and overlays. var mapsInitMarkers = ; // Markers builder callback function for init js-var function build_markers_array() { $output = ''; $markers = array( /* DEFINE MARKERS HERE */ ); foreach ( $markers as $marker ) { // id, lat, lng, name $output .= "[ {$result['ID']$marker['ID']}, {$result['lat']$marker['lat']}, {$result['lng']$marker['lng']}, '{$result['name']$marker['name']}', ],"; } $output = substr( $output, 0, -1 ); // get rid of last comma $output = "[{$output}]"; return $output; }

The Ajax Callback function

function ajax_cb_fn() { check_ajax_referer( 'referer_name', $ajax_nonce ); // needed if performed some update via some ex. form
    $output = ''; // do stuff here - example, use build_markers_array(); with some input to update your markers

    $response = json_encode( array( 
         'data' => $output 
    ) );
    header( "Content-Type: application/json" );
    echo $response;

    exit();
}

The data handling inside ajax_json.js

Now you can write some `document.ready()` function inside your new ajax json handling file. In there you can access your data via the `ajax_object` javascript object. You could use `$.post()` to perform data handling via forms, or else. You can also bump your markers into some `createMarkers` js function which you could also use to build your initial array (you need to handle the marker creation somewhere.

Why you shouldn't add this data as mark-up element attributes

Mark up get's rendered by the browser, data just processed. Just imagine how your div will look like if you drop hundreds of lat/lng atts in there. In a short time you'll need to drop support for device browsers and netbook laptops, tablets, etc.

You need to have some initial (maybe global) js var markers_array = [];. Then use an ajax callback function to bump your markers into that array.

Register your scripts

// Define $root_url = plugin_dir_path(); // Example path $ajax_nonce = 'your_nonce'; $ajax_nonce_val = 'your_val'; // Ajax wp_register_script( 'json2', false, false, false, true ); wp_enqueue_script( 'json2' ); wp_enqueue_script( 'ajax-script' ,"{$root_url}js/ajax_json.js" // your ajax callback script that handles the request (inside a /js folder) ,array( 'json2' ) ,false ,true ); wp_localize_script( 'ajax-script' ,'ajax_object' // YOUR MAIN DATA OBJECT ,array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ,$ajax_nonce => wp_create_nonce( $ajax_nonce_val ) ) );

The initial Markers

// inside some part: You need the following php function to build your markers array: // This js array will be used by your custom build markers js function to build the initial markers. It can also be used to update markers and overlays. var mapsInitMarkers = ; // Markers builder callback function for init js-var function build_markers_array() { $output = ''; $markers = array( /* DEFINE MARKERS HERE */ ); foreach ( $markers as $marker ) { // id, lat, lng, name $output .= "[ {$result['ID']}, {$result['lat']}, {$result['lng']}, '{$result['name']}', ],"; } $output = substr( $output, 0, -1 ); // get rid of last comma $output = "[{$output}]"; return $output; }

The Ajax Callback function

function ajax_cb_fn() { check_ajax_referer( 'referer_name', $ajax_nonce ); // needed if performed some update via some ex. form
    $output = ''; // do stuff here - example, use build_markers_array(); with some input to update your markers

    $response = json_encode( array( 
         'data' => $output 
    ) );
    header( "Content-Type: application/json" );
    echo $response;

    exit();
}

The data handling inside ajax_json.js

Now you can write some `document.ready()` function inside your new ajax json handling file. In there you can access your data via the `ajax_object` javascript object. You could use `$.post()` to perform data handling via forms, or else. You can also bump your markers into some `createMarkers` js function which you could also use to build your initial array (you need to handle the marker creation somewhere.

Why you shouldn't add this data as mark-up element attributes

Mark up get's rendered by the browser, data just processed. Just imagine how your div will look like if you drop hundreds of lat/lng atts in there. In a short time you'll need to drop support for device browsers and netbook laptops, tablets, etc.

You need to have some initial (maybe global) js var markers_array = [];. Then use an ajax callback function to bump your markers into that array.

Register your scripts

// Define $root_url = plugin_dir_path(); // Example path $ajax_nonce = 'your_nonce'; $ajax_nonce_val = 'your_val'; // Ajax wp_register_script( 'json2', false, false, false, true ); wp_enqueue_script( 'json2' ); wp_enqueue_script( 'ajax-script' ,"{$root_url}js/ajax_json.js" // your ajax callback script that handles the request (inside a /js folder) ,array( 'json2' ) ,false ,true ); wp_localize_script( 'ajax-script' ,'ajax_object' // YOUR MAIN DATA OBJECT ,array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ,$ajax_nonce => wp_create_nonce( $ajax_nonce_val ) ) );

The initial Markers

// inside some part: You need the following php function to build your markers array: // This js array will be used by your custom build markers js function to build the initial markers. It can also be used to update markers and overlays. var mapsInitMarkers = ; // Markers builder callback function for init js-var function build_markers_array() { $output = ''; $markers = array( /* DEFINE MARKERS HERE */ ); foreach ( $markers as $marker ) { // id, lat, lng, name $output .= "[ {$marker['ID']}, {$marker['lat']}, {$marker['lng']}, '{$marker['name']}', ],"; } $output = substr( $output, 0, -1 ); // get rid of last comma $output = "[{$output}]"; return $output; }

The Ajax Callback function

function ajax_cb_fn() { check_ajax_referer( 'referer_name', $ajax_nonce ); // needed if performed some update via some ex. form
    $output = ''; // do stuff here - example, use build_markers_array(); with some input to update your markers

    $response = json_encode( array( 
         'data' => $output 
    ) );
    header( "Content-Type: application/json" );
    echo $response;

    exit();
}

The data handling inside ajax_json.js

Now you can write some `document.ready()` function inside your new ajax json handling file. In there you can access your data via the `ajax_object` javascript object. You could use `$.post()` to perform data handling via forms, or else. You can also bump your markers into some `createMarkers` js function which you could also use to build your initial array (you need to handle the marker creation somewhere.

Why you shouldn't add this data as mark-up element attributes

Mark up get's rendered by the browser, data just processed. Just imagine how your div will look like if you drop hundreds of lat/lng atts in there. In a short time you'll need to drop support for device browsers and netbook laptops, tablets, etc.
added 322 characters in body
Source Link
kaiser
  • 50.8k
  • 27
  • 150
  • 244

You need to have some initial (maybe global) js var markers_array = [];. Then use an ajax callback function to bump your markers into that array.

Register your scripts

// Define $root_url = plugin_dir_path(); // Example path $ajax_nonce = 'your_nonce'; $ajax_nonce_val = 'your_val'; // Ajax wp_register_script( 'json2', false, false, false, true ); wp_enqueue_script( 'json2' ); wp_enqueue_script( 'ajax-script' ,"{$root_url}js/ajax_json.js" // your ajax callback script that handles the request (inside a /js folder) ,array( 'json2' ) ,false ,true ); wp_localize_script( 'ajax-script' ,'ajax_object' // YOUR MAIN DATA OBJECT ,array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ,$ajax_nonce => wp_create_nonce( $ajax_nonce_val ) ) );

The initial Markers

// inside some part: You need the following php function to build your markers array: // This js array will be used by your custom build markers js function to build the initial markers. It can also be used to update markers and overlays. var mapsInitMarkers = ; // Markers builder callback function for init js-var function build_markers_array() { $output = ''; $markers = array( /* DEFINE MARKERS HERE */ ); foreach ( $markers as $marker ) { // id, lat, lng, name $output .= "[ {$result['ID']}, {$result['lat']}, {$result['lng']}, '{$result['name']}', ],"; } $output = substr( $output, 0, -1 ); // get rid of last comma $output = "[{$output}]"; return $output; }

The Ajax Callback function

function ajax_cb_fn() { check_ajax_referer( 'referer_name', $ajax_nonce ); // needed if performed some update via some ex. form
    $output = ''; // do stuff here - example, use build_markers_array(); with some input to update your markers

    $response = json_encode( array( 
         'data' => $output 
    ) );
    header( "Content-Type: application/json" );
    echo $response;

    exit();
}

The data handling inside ajax_json.js

Now you can write some `document.ready()` function inside your new ajax json handling file. In there you can access your data via the `ajax_object` javascript object. You could use `$.post()` to perform data handling via forms, or else. You can also bump your markers into some `createMarkers` js function which you could also use to build your initial array (you need to handle the marker creation somewhere.

Why you shouldn't add this data as mark-up element attributes

Mark up get's rendered by the browser, data just processed. Just imagine how your div will look like if you drop hundreds of lat/lng atts in there. In a short time you'll need to drop support for device browsers and netbook laptops, tablets, etc.

You need to have some initial (maybe global) js var markers_array = [];. Then use an ajax callback function to bump your markers into that array.

Register your scripts

// Define $root_url = plugin_dir_path(); // Example path $ajax_nonce = 'your_nonce'; $ajax_nonce_val = 'your_val'; // Ajax wp_register_script( 'json2', false, false, false, true ); wp_enqueue_script( 'json2' ); wp_enqueue_script( 'ajax-script' ,"{$root_url}js/ajax_json.js" // your ajax callback script that handles the request (inside a /js folder) ,array( 'json2' ) ,false ,true ); wp_localize_script( 'ajax-script' ,'ajax_object' // YOUR MAIN DATA OBJECT ,array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ,$ajax_nonce => wp_create_nonce( $ajax_nonce_val ) ) );

The initial Markers

// inside some part: You need the following php function to build your markers array: // This js array will be used by your custom build markers js function to build the initial markers. It can also be used to update markers and overlays. var mapsInitMarkers = ; // Markers builder callback function for init js-var function build_markers_array() { $output = ''; $markers = array( /* DEFINE MARKERS HERE */ ); foreach ( $markers as $marker ) { // id, lat, lng, name $output .= "[ {$result['ID']}, {$result['lat']}, {$result['lng']}, '{$result['name']}', ],"; } $output = substr( $output, 0, -1 ); // get rid of last comma $output = "[{$output}]"; return $output; }

The Ajax Callback function

function ajax_cb_fn() { check_ajax_referer( 'referer_name', $ajax_nonce ); // needed if performed some update via some ex. form
    $output = ''; // do stuff here - example, use build_markers_array(); with some input to update your markers

    $response = json_encode( array( 
         'data' => $output 
    ) );
    header( "Content-Type: application/json" );
    echo $response;

    exit();
}

The data handling inside ajax_json.js

Now you can write some `document.ready()` function inside your new ajax json handling file. In there you can access your data via the `ajax_object` javascript object. You could use `$.post()` to perform data handling via forms, or else. You can also bump your markers into some `createMarkers` js function which you could also use to build your initial array (you need to handle the marker creation somewhere.

You need to have some initial (maybe global) js var markers_array = [];. Then use an ajax callback function to bump your markers into that array.

Register your scripts

// Define $root_url = plugin_dir_path(); // Example path $ajax_nonce = 'your_nonce'; $ajax_nonce_val = 'your_val'; // Ajax wp_register_script( 'json2', false, false, false, true ); wp_enqueue_script( 'json2' ); wp_enqueue_script( 'ajax-script' ,"{$root_url}js/ajax_json.js" // your ajax callback script that handles the request (inside a /js folder) ,array( 'json2' ) ,false ,true ); wp_localize_script( 'ajax-script' ,'ajax_object' // YOUR MAIN DATA OBJECT ,array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ,$ajax_nonce => wp_create_nonce( $ajax_nonce_val ) ) );

The initial Markers

// inside some part: You need the following php function to build your markers array: // This js array will be used by your custom build markers js function to build the initial markers. It can also be used to update markers and overlays. var mapsInitMarkers = ; // Markers builder callback function for init js-var function build_markers_array() { $output = ''; $markers = array( /* DEFINE MARKERS HERE */ ); foreach ( $markers as $marker ) { // id, lat, lng, name $output .= "[ {$result['ID']}, {$result['lat']}, {$result['lng']}, '{$result['name']}', ],"; } $output = substr( $output, 0, -1 ); // get rid of last comma $output = "[{$output}]"; return $output; }

The Ajax Callback function

function ajax_cb_fn() { check_ajax_referer( 'referer_name', $ajax_nonce ); // needed if performed some update via some ex. form
    $output = ''; // do stuff here - example, use build_markers_array(); with some input to update your markers

    $response = json_encode( array( 
         'data' => $output 
    ) );
    header( "Content-Type: application/json" );
    echo $response;

    exit();
}

The data handling inside ajax_json.js

Now you can write some `document.ready()` function inside your new ajax json handling file. In there you can access your data via the `ajax_object` javascript object. You could use `$.post()` to perform data handling via forms, or else. You can also bump your markers into some `createMarkers` js function which you could also use to build your initial array (you need to handle the marker creation somewhere.

Why you shouldn't add this data as mark-up element attributes

Mark up get's rendered by the browser, data just processed. Just imagine how your div will look like if you drop hundreds of lat/lng atts in there. In a short time you'll need to drop support for device browsers and netbook laptops, tablets, etc.
Source Link
kaiser
  • 50.8k
  • 27
  • 150
  • 244

You need to have some initial (maybe global) js var markers_array = [];. Then use an ajax callback function to bump your markers into that array.

Register your scripts

// Define $root_url = plugin_dir_path(); // Example path $ajax_nonce = 'your_nonce'; $ajax_nonce_val = 'your_val'; // Ajax wp_register_script( 'json2', false, false, false, true ); wp_enqueue_script( 'json2' ); wp_enqueue_script( 'ajax-script' ,"{$root_url}js/ajax_json.js" // your ajax callback script that handles the request (inside a /js folder) ,array( 'json2' ) ,false ,true ); wp_localize_script( 'ajax-script' ,'ajax_object' // YOUR MAIN DATA OBJECT ,array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ,$ajax_nonce => wp_create_nonce( $ajax_nonce_val ) ) );

The initial Markers

// inside some part: You need the following php function to build your markers array: // This js array will be used by your custom build markers js function to build the initial markers. It can also be used to update markers and overlays. var mapsInitMarkers = ; // Markers builder callback function for init js-var function build_markers_array() { $output = ''; $markers = array( /* DEFINE MARKERS HERE */ ); foreach ( $markers as $marker ) { // id, lat, lng, name $output .= "[ {$result['ID']}, {$result['lat']}, {$result['lng']}, '{$result['name']}', ],"; } $output = substr( $output, 0, -1 ); // get rid of last comma $output = "[{$output}]"; return $output; }

The Ajax Callback function

function ajax_cb_fn() { check_ajax_referer( 'referer_name', $ajax_nonce ); // needed if performed some update via some ex. form
    $output = ''; // do stuff here - example, use build_markers_array(); with some input to update your markers

    $response = json_encode( array( 
         'data' => $output 
    ) );
    header( "Content-Type: application/json" );
    echo $response;

    exit();
}

The data handling inside ajax_json.js

Now you can write some `document.ready()` function inside your new ajax json handling file. In there you can access your data via the `ajax_object` javascript object. You could use `$.post()` to perform data handling via forms, or else. You can also bump your markers into some `createMarkers` js function which you could also use to build your initial array (you need to handle the marker creation somewhere.