Skip Navigation

[Resolved] Call "Views-api" methods through PHP from another custom plugin

This support ticket is created 4 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 1 reply, has 2 voices.

Last updated by Minesh 4 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#1662101

Tell us what you are trying to do?
Create a custom plugin to enable a button shortcode (or function) that when used to a page (or page button) that will create a PDF with the data from some views with the use of the FPDF (hidden link) which will be downloaded by the user.

Is there any documentation that you are following?
https://toolset.com/documentation/programmer-reference/views-api/#render_view
hidden link

Right now I have created the code for creating a simple PDF form but when I call the "render_view()" I get the error

<?php
/*
* Plugin Name: My Custom PDF Creator with FPDF
* Description: A plugin created to build a PDF document.
* Version: 1.0
* Author: Sotiris Krontiris
* Author URI: <em><u>hidden link</u></em>
*/

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

include( 'my-pdf-creator-helper-functions.php');
$pdf = new PDF_HTML();

if( isset($_POST['generate_posts_pdf'])){
    output_pdf();
}

add_action( 'admin_menu', 'as_fpdf_create_admin_menu' );
function as_fpdf_create_admin_menu() {
    $hook = add_submenu_page(
        'tools.php',
        'My Custom PDF Generator',
        'My Custom PDF Generator',
        'manage_options',
        'as-fdpf-tutorial',
        'as_fpdf_create_admin_page'
    );
}

function output_pdf() {
   
   /*
    $posts = get_posts( 'posts_per_page=5' );

    if( ! empty( $posts ) ) {
        global $pdf;
        $title_line_height = 10;
        $content_line_height = 8;

        $pdf->AddPage();
        $pdf->SetFont( 'Arial', '', 42 );
        $pdf->Write(20, 'VESSELJOIN.COM');

        foreach( $posts as $post ) {

            $pdf->AddPage();
            $pdf->SetFont( 'Arial', '', 22 );
            $pdf->Write($title_line_height, $post->post_title);

            // Add a line break
            $pdf->Ln(15);

            // Image
            $page_width = $pdf->GetPageWidth() - 20;
            $max_image_width = $page_width;

            $image = get_the_post_thumbnail_url( $post->ID );
            if( ! empty( $image ) ) {
                $pdf->Image( $image, null, null, 100 );
            }
            
            // Post Content
            $pdf->Ln(10);
            $pdf->SetFont( 'Arial', '', 12 );
            $pdf->WriteHTML($post->post_content);
        }
    }
    */

    global $pdf;
    $title_line_height = 10;
    $content_line_height = 8;

    $pdf->AddPage();
    $pdf->SetFont( 'Arial', '', 42 );
    $pdf->Write(20, '.COM');

    // Add a line break
    $pdf->Ln(15);

    // Image
    $page_width = $pdf->GetPageWidth() - 20;
    $max_image_width = $page_width;

    //$image = get_the_post_thumbnail_url( $post->ID );
    //if( ! empty( $image ) ) {
    //    $pdf->Image( $image, null, null, 100 );
    //}
    
    //Display Personal Details
    $args = array(
        'name' => 'print-personal-data'
    );
    
    $pdf->Ln(10);
    $pdf->SetFont( 'Arial', '', 12 );
    //$pdf->WriteHTML(render_view( $args ));
    //$pdf->Write(render_view( array( 'id' => 57530 ) ));
    $view_data = render_view( array( 'id' => 57530 ) );
    $viewdata_returned = sprintf($view_data);
    $pdf->WriteHTML( $viewdata_returned );
    
    $pdf->Output('D','cv-form.pdf');
    exit;
}


function as_fpdf_create_admin_page() {
?>
<div class="wrap">
    <h1>My Custom - WordPress PDF Generator</h1>
    <p>Click below to generate a pdf from the content inside all the WordPress Posts. </p>
    <p>Each post will be on its own pdf page containing the post title and post content.</p>
	<form method="post" id="as-fdpf-form">
        <button class="button button-primary" type="submit" name="generate_posts_pdf" value="generate">Generate PDF from WordPress Posts</button>
    </form>
</div>
<?php
}

########Error!
[14-Jun-2020 12:06:16 UTC] PHP Fatal error: Uncaught Error: Call to undefined function render_view() in /var/www/wp-content/plugins/my-pdf-creator/my-pdf-creator-tutorial.php:102
Stack trace:
#0 /var/www/wp-content/plugins/my-pdf-creator/my-pdf-creator-tutorial.php(18): output_pdf()
#1 /var/www/wp-settings.php(371): include_once('/var/www/wp-con...')
#2 /var/www/wp-config.php(126): require_once('/var/www/wp-set...')
#3 /var/www/wp-load.php(37): require_once('/var/www/wp-con...')
#4 /var/www/wp-admin/admin.php(34): require_once('/var/www/wp-loa...')
#5 /var/www/wp-admin/tools.php(40): require_once('/var/www/wp-adm...')
#6 {main}
thrown in /var/www/wp-content/plugins/my-pdf-creator/my-pdf-creator-tutorial.php on line 102

The credentials of the site are in the below link:
https://toolset.com/forums/topic/filtering-view-with-two-complex-relationships/#post-1637351

#1663147

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

The first thing is we do not support custom code as it beyond the scope of our support policy however as you are using view's PHP API function render_views() and it seems to me that you are using/calling the function before the views in initialized.

Probably you should try to call the render_view() function on some hook rather on plugin activation.

As you can see with the related ticket, I've used the wp_loaded and I was able to call the view's PHP API function get_view_query_results() successfully.
=> https://toolset.com/forums/topic/big-problem-with-view-2/#post-1227849

Can you please try to call the function on wp_loaded() and check if that help you to resolve your issue.