How to generate (DOM) PDF in WordPress post editing screen? - php

Quite new to (WordPress) PHP and trying to generate a custom post invoice via an extra metabox and with the help of the Dompdf library. I found a fix for the front end, but now I'm trying to convert WordPress admin custom post editing screen and here I'm getting stuck.
I've found a question on here that basically describes the same question and answer - but for the frontend.
[https://stackoverflow.com/questions/38723772/how-to-use-dompdf-with-wordpress]
My question is: would the same approach - suggested by Matteo Enna - work for the custom post type editing screen in wp admin?
Thanks all, have a great day.
UPDATE
This is the code I have for my metabox callback function, but I'm getting a blank screen upon on running the 'stream' method (it seems to work up until the 'render' method).
function invoice_metabox_callback() {
require plugin_dir_path( __FILE__ ) . '/dompdf/autoload.inc.php';
$dompdf = new Dompdf\Dompdf;
$dompdf->loadHtml('Hello world');
$dompdf->render();
$dompdf->stream();
}
UPDATE
The problem seems to be WordPress admin specific. I've used the code above to run the script on the frontend.
UPDATE
Furthermore, specically streaming the document in the WordPress admin area is not working. Saving the Dompdf output to a file in the plugin directory works fine.

Related

How to display custom text below site logo image with function / filter in wordpress

I needed to display my site slogan (about four words) below the site logo image, my theme author told me to add the text after certain codes in header.php. This works but my worry is that the change will be overridden after theme update because i don't use child them, i prefer using code snippet plugin to add code to my site.
I need a function / filter that i can add to my function.php to enable me achieve this without losing the change after theme update.
Can anybody help with this?
Install the PHP Code Snippet addon for WordPress, then you can put php code wherever you want on your site inline:
https://wordpress.org/plugins/insert-php-code-snippet/
From the developers site:
Insert PHP Code Snippet allows you to create shortcodes corresponding to PHP code snippets. You can create a shortcode corresponding to any random PHP code such as ad codes, login validation etc. and use the same in your posts, pages or widgets.
The shortcodes generated using the plugin are easily available as a dropdown in the standard wordpress content editor as well as in widget settings, thereby giving you ease of integrating your PHP snippets with your posts and pages.
Users Guide:
https://help.xyzscripts.com/docs/insert-php-code-snippet/user-guide/
Download link:
https://downloads.wordpress.org/plugin/insert-php-code-snippet.1.3.2.zip

Wordpress - creating custom pages with php code

I need to add few additional pages to my wordpress site.
These pages should not be "part of the site", ie they should not be linked somewhere from the posts and so on.
However, they should have the same header/footer as the rest of the site ( I am using custom theme ). And they should be accessible via url.
The final requirement is, I should be able to code in php.
At the moment, I tried to create a new "Page" in my admin console. And then write some php-code inside. However, all my php code gets commented and since not executed.
I don't think that installing plugins such as Exec-PHP is a good idea, so I am trying to find other solutions.
Any comments/advice/suggestions how to make it?
I would be grateful if you give me some how-to link.
Thank you in advance.
You can use a custom page template in your theme for this and just keep the site empty in the admin panel:
http://codex.wordpress.org/Theme_Development#Custom_Page_Templates
By using Template tag's you can create custom page.
write this code on the top of your php file....
/*
Template Name: Your Template Name
*/

How to save PHP code written in Wordpress page

I'm using Artisteer to create WordPress themes.
Artisteer is not able to include php code into pages, then I have to do some modification via WordPress page editor.
I'm using "PHP Insert" plugin to include php code into WordPress page editor and to make WordPress able to use my php code into my pages.
In my site I have a form for B.M.I. calculation and I need to use php code in the page.
The problem is that if I change the theme in Artisteer and if I upload it into WordPress, I loose php changes.
It's very boring to apply modification every time (and also dangerous...lost code, wrong code position,...).
I'm thinking to use a child theme, but I don't know how to apply it to php code added into pages.
Suggestions?
Thanks in advance
The Wordpress team has made it a rule of thumb to never execute PHP in posts and pages either because it can be hectic to change things in the future and may expose security vulnerabilities that could not be fixed by Wordpress itself.
A better practice is editing your WordPress theme by creating a custom post/page template - that basically can include everything. However for some situations, it may be necessary to execute PHP on more than one WordPress posts/pages.
In this case, my recommendation would be to create a simple page template containing your PHP, or, if your code is used multiple times, create a shortcode (in the functions.php of your template).
Simplest example of adding a shortcode tag (with parameters) to your themes functions.php:
function footag_func( $atts ) {
return "foo = {$atts['foo']}";
}
add_shortcode('footag', 'footag_func');
The output code can be used everywhere with the tag [footag foo="bar"]. When extending this, alway remember to return the results of your function. Using echo or print would just echo the results (on the top of your content, skipping the content itself).
You're not supposed to use PHP in the content editor, you've got a few decent options:
Creating a custom template for that specific page:
http://codex.wordpress.org/Page_Templates
Creating a shortcode for that specific piece of PHP code:
http://codex.wordpress.org/Shortcode_API

How to update my Wordpress 3.0 widget admin page using the update function or equivalent?

I originally wrote my Wordpress 3.0 widget http://codex.wordpress.org/Widgets_API for editing on the widget page after it has been placed in a sidebar, which handles the updating automatically using the update() function in widget. It has grown and needs a widget admin page now so outside my widget class I added another action I found http://codex.wordpress.org/Adding_Administration_Menus to add a admin_menu, so far so good (hopefully I did it using a current API method but there didn't seem to another method in the widget.php file of WP). I created the form but I don't know how to update the widget now. I've looked through the code for widgets and I'm not sure how the update function actually works. I read that you can have options and add them to the database but if they already exist I don't want to abandon that data if there's a way to keep using it. The widget will keep two fields on the widget page and the rest will be handled on the admin page.
How do I update my widget from the admin page and have it be retrieved by the widget and can that be done in such a way that it is stored in the same place as my existing widget data using WP3.0 current widget API? If not how would I go about doing it?
The answer can be found in the "Plugin Options Starter Kit" plugin http://wordpress.org/extend/plugins/plugin-options-starter-kit/ that contains all you need to figure out how to use the settings API, adding admin menu pages and more. Can't wait to see the next update of this plugin.

WP Plugin: Adding a custom code in editing post and rendering it

I am new into WP Plugin development. I want to make a plugin which adds the facility to use any custom code within editor to render it into something else. For instance if I am referring my Hacker News profie and I am using the code H(HackerNewsID) then after publishing it makes a URL of Hacker News pointing to my profile.
What feature of WP would be best fit for it?
Thanks
You cand try shortcodes http://codex.wordpress.org/Shortcode_API

Categories