-3

I'm currently creating my own plugin to window.print() a post on my WordPress website. But it seems that I cannot access the function thru the onclick. If I put the window.print() in the button itself it works, but that is not how it has to work for me.

function wpb_follow_us($content) {
  // Only do this when a single post is displayed
  if ( is_single() ) {  
    // Message you want to display after the post
    $content .= '<button type="Submit" value="Print_PDF" 
    onclick="GetPDF()"> Print PDF </button>';
  } 
  // Return the content
  return $content;  
}

But whenever I click the button I get an error that says that it does not access this function:

function GetPDF() {
   window.print();
}

It is in the same file.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Why don't you put window.print() inside the onclick like onclick="window.print();" itself? Also, don't wrap the GetPDF() in jquery document ready function. – Vinay Jain Sep 06 '22 at 10:05
  • The GetPDF is a js function and not a PHP function right? – Vinay Jain Sep 06 '22 at 10:07
  • I meant to ask if it's a Javascript function or a PHP function. – Vinay Jain Sep 06 '22 at 10:09
  • And it's not wrapped inside the jQuery's $( document ).ready() function right? – Vinay Jain Sep 06 '22 at 10:10
  • 1
    Maybe change `type="submit"` to `type="button"` – geertjanknapen Sep 06 '22 at 10:12
  • 2
    As per your code GetPDF() is a PHP function here and not the JS function. (Contrary to what you said). You can't call PHP functions from onclick – Vinay Jain Sep 06 '22 at 10:15
  • i cant ben stephens. since some people downvotes some relevant questions of mine, SO told me to change my current questions, so i did. but i returned it to the old state – Delano van londen Dec 13 '22 at 09:14
  • That's fine, what Ben Stephens wrote you should be aware of and merely a suggestion, they likely don't knew about the general quality of your questions (by the votes, not a judgment call of me personally). Best hint I may give probably is that "not working" is not a good problem description and tends to make questions needlessly broad. You normally have a concrete problem, right? What did not work? What was expected? What happend instead? Questions like these can help to make a question more relevant. More is in the [help]. – hakre Dec 13 '22 at 09:26
  • i see what you are saying, and i will take the hint with you for future questions. appreciate it – Delano van londen Dec 13 '22 at 09:29
  • its fine, there was no reason you could know – Delano van londen Dec 13 '22 at 09:47

2 Answers2

1

Try this:

// Only do this when a single post is displayed
if ( is_single() ) { ?>

<script>
function GetPDF(e) {
    e.preventDefault();
    window.print();
}
</script>
 
<?php 
    // Message you want to display after the post

    $content .= '<button type="button" value="Print_PDF" onclick="GetPDF(event)"> Print 
    PDF </button>';
 
    } 
    // Return the content
    return $content;   
    
}
Vinay Jain
  • 862
  • 1
  • 3
  • 11
0

The HTML element's onclick attribute looks for a javascript function, it cannot run a PHP function.

See this question

Good guy
  • 34
  • 1
  • 3