0

So I was able to save the content of each post in an array, thanks to a very good answer on it: How to localize value of posts

I just copied the solution here:

function register_and_enqueue_script()
{
    if('myPostType' == get_post_type() && have_posts()) {

    wp_register_script( 'js_script', plugin_dir_url(__FILE__).'js/script.js', array(), '1.8.5' );
    wp_enqueue_script('js_script');

    $myCustomValue = array();
    while(have_posts()) {
        the_post();
        $mypostid = get_the_ID();
        $myCustomValue[''+$mypostid] = nl2br(get_post_meta($mypostid, 'custom_value', true));
    } // end while
    rewind_posts();

    wp_localize_script('js_script', 'myCustomValue', $myCustomValue);

} // end if

But now I have a new problem: How do I make sure, that only the content of the actual post, where I did the input, is changing? I somehow need to be able to pass the post_id to my JS-Function. But how?

This is the call of the Javascript function:

function modify_custom_value()
{
    return '<div>
                <div>
                    <input type="input" onkeyup="doFunction();" "/>
                </div>
            </div>' ;
}

This is the function in JS:

function doFunction(){
     //alter the content of the post
}

Here I do call somefunction():

function extend_posts( $content ) 
{
    if ( 'myPostType' == get_post_type() )
    {
        if( $meta = get_post_meta( get_the_ID(), 'custom_value', true ) ) {
           $content = $content . implement_custom_value(); //this works fine
           $content = $content . modify_custom_value(); //this just dont
        }
    }
    return $content;
}
add_filter( 'the_content', 'extend_posts' );

Any Ideas?

I'm sorry for my many questions :/

8
  • is there an input for every post? if so, why not add the input to your template, so you can pass the id as a parameter? – inarilo Jun 3 '17 at 16:10
  • Yes, in every post. I want it to be completley independent of the template. It's a plugin. – TheOnionMaster Jun 3 '17 at 16:19
  • you can use this codex.wordpress.org/Plugin_API/Filter_Reference/the_content – inarilo Jun 3 '17 at 16:41
  • I've just edited the post. – TheOnionMaster Jun 4 '17 at 9:12
  • what does modify_custom_value do? are you getting any php errors? – inarilo Jun 4 '17 at 12:50
0

If you want to add the post id to your javaScript, i would think of 2 methods.

Output a variable and then retrieve it using javascript

Let's say you need the post it. You can create a hidden div, and assign the value of your post id to it:

<div id="my-post-id"><?php echo get_the_ID(); ?></div>

Now you can retrieve this in your script by using this:

 var x = document.getElementById("my-post-id").textContent; 

You have your post id now, proceed to further steps.

Use wp_add_inline_script() to output your script

You can directly add javaScript code to your header or footer by using wp_add_inline_script(). Take a look at this example:

wp_add_inline_script('js_script','alert("'.get_the_date().'");');

Now you can directly use php functions to output contents, use in loops, and more. However you need to enqueue a JS file first and then hook your inline script to it ( in this case, a script that has the ID of js-script is being used for hooking ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.