Saving gravity PDF to local computer - wordpress

I'm trying to figure out if there is a way to get the PDF generated from gravity PDF saved locally or access the URL of the PDF created in a variable. Is this possible? I found https://gravitypdf.com/documentation/v4/development-helper-parameters/ and it seems promising but not sure how to access the URL generated.

Piece of code in question (not the sexiest code but worked at the time)
Basically drop the shortcode [custom_pdf_entries] on a specific page that your user access, this will look for all the entries and look for a gravitypdf ID to pull the formated PDF generated with the entry in question.
add_shortcode("custom_pdf_entries", array($this,'custom_get_form_entry'));
/*
* Get All PDF from Form exclude those we dont want
*/
function custom_get_form_entry($atts){
$search_arg = array(
'status' => 'active',
'field_filters' => array(
'mode' => 'all',
array(
'key' => 'created_by',
'value' => get_current_user_id()
)
)
);
$sorting = array(
'key' => '1',
'direction' => 'ASC'
);
$all_forms = GFAPI::get_forms();
$excluded_id = explode("," , $atts['exclude']);
ob_start();
foreach($all_forms as $key => $a_val){
if( !in_array($a_val['fields'][0]->formId, $excluded_id) ){
$entries = GFAPI::get_entries($a_val['fields'][0]->formId,$search_arg,$sorting);
$count = 1;
if(count($a_val['gfpdf_form_settings']) > 0){
$gravity_pdf_form_id = current(array_keys($a_val['gfpdf_form_settings']));
if($entries){
foreach($entries as $key => $val){
echo do_shortcode('[gravitypdf name="Forms" type="download" id="'.$gravity_pdf_form_id.'" text="'.$a_val['title'].' #'.$count.'" entry="'.$val['id'].'"]');
echo '<br/><br/>';
$count++;
}
}
}
}
}
return ob_get_clean();
}
Note: some modification maybe required also i can probably help you if anything arise

Related

Show Authors in a Page Based on Selected Metadata

I'm working on a new project where registered users can select multiple careers in the User Profile page in wordpress Admin. I have been able to add the check boxes of different careers to the User profile page which registered users can check/select.
At the frontend, I created a page for each of the careers. I want a scenerio whereby if one should go to page named "Programmer" for example, one would be shown all the authors that check "Programmer" in their profile. The authors info like avatar, name and links to all posts would be shown in a loop
I don't know how to go about it. Hoping you guys here can assist with this.
-------------------------------EDITED---------------------------
With the help provided by #AustinWinstanley thus far, here is my code
$user_query = new WP_User_Query( array( 'meta_key' => 'career', 'meta_value' => 'programmer' ) );
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo '<p>' . $user->display_name . '</p>';
}
} else {
echo 'No users found.';
}
With the above query, any author whose career field's value is programmer is listed. The above code is suitable if the value field for Career is an input field. I don't intend to make career's value to be an input field, rather I want it to be checkboxes where authors can check multiple values.
Since it's not going to be an input field, but rather checkboxes, how do I edit this line to reflect what I want?
$user_query = new WP_User_Query( array( 'meta_key' => 'career', 'meta_value' => 'programmer' ) );
You probably want to query users by meta data. Something like this:
// Set this value by whatever you're searching for
$career = 'programmer';
$args = array(
'meta_query' => array(
'relation' => 'AND',
array(
array(
'key' => 'career',
'value' => $career,
'compare' => 'LIKE'
// Or 'compare' => '='
)
)
)
);
$user_query = new WP_User_Query( $args );
$result = $wp_user_query->get_results();
Edit:
For the front end form:
<form method='post'>
<input name="careers[]" type="checkbox" value="programmer"> Programmer
<input name="careers[]" type="checkbox" value="accountant"> Accountant
<input name="careers[]" type="checkbox" value="manager"> Manager
<button name="career-search">Search</button>
</form>
In your plugin or function/includes:
function careers_postback( $query ) {
if( $query->is_main_query() && isset( $_POST['career-search'] ) ) {
$careers = $_POST['careers']
$meta_query = array();
foreach( $careers as $career ) {
$meta_query[] = array(
'relation' => 'AND',
array(
array(
'key' => 'career',
'value' => $career,
'compare' => '='
)
)
);
}
$args = array(
'meta_query' => $meta_query
);
$user_query = new WP_User_Query( $args );
$result = $wp_user_query->get_results();
set_query_var( 'search-career-results', $result );
// Here you would include your search template and call exit
// In your template, use `get_query_var( 'search-career-results')
// Then check if there are results and list them if there are.
}
}
add_filter( 'pre_get_posts', 'careers_postback' );
This is in general. I haven't fully tested it because I'm not typing it in an editor. But this is the full solution to check boxes and get users by their meta data using the data. If this doesn't work in general, then you are doing something you aren't revealing. Like not saving careers to user meta data.

Creating a page with wp_insert_post()

I have a form that collects data about a user's product and then creates a page in WordPress with
$my_post = array(
'post_content' => "My page content",
'post_title' => $product_title,
'post_name' => $product_title,
'post_type' => 'page', // must be 'page' to accept the 'page_template' below
'page_template' => "listing.php",
'post_status' => "publish"
);
$ID = wp_insert_post( $my_post );
$permalink = get_permalink($ID);
echo "<br />ID for new page is $ID, Permalink for new page is $permalink";
The form data is put into meta variables for the page ID and the listing.php template file pulls it out of there and builds the HTML to display the product page. This all works fine and I can see that the page meta variable, _wp_page_template, gets set correctly to the template file I specified, listing.php:
Now I want to create a second page from the same form data, this one displaying different parts of the data in a different way. So I've added a second block of code, starting at $my_cert below, that creates this second page and specifies a different template, certificate.php, that knows how to build the second version of the data.
$my_post = array(
'post_content' => "My page content",
'post_title' => $product_title,
'post_name' => $product_title,
'post_type' => 'page', // must be 'page' to accept the 'page_template' below
'page_template' => "listing.php",
'post_status' => "publish"
);
$ID = wp_insert_post( $my_post );
$permalink = get_permalink($ID);
echo "<br />ID for new page is $ID, Permalink for new page is $permalink";
$my_cert = array(
'post_content' => "My certificate", // post_content is required
'post_title' => "My certificate", // post_title is required
'post_name' => "My certificate",
'post_type' => 'page', // must be 'page' to accept the 'page_template' below
'page_template' => "certificate.php",
'post_status' => "publish"
);
$CERT_ID = wp_insert_post( $my_cert );
$cert_permalink = get_permalink($CERT_ID);
echo "<br />ID for new certificate is $CERT_ID, Permalink for new certificate is $cert_permalink";
But when I look in the meta data for the second page created, the template is set to "default" instead of certificate.php:
I know I've set up certificate.php correctly as a template (set /* Template Name: certificate */ at the top) because the Page Edit Template dropdown includes certificate:
So does anyone see why I can't create this second page with the template set to certificate.php?
Thanks
Are you sure your page template src for: certificate.php is: certificate.php? And not: templates/certificate.php or something like that. Look in your theme folder and be 100% of the page template path. Check your spelling or for typos in the page template path or name. It must be an exact match.
If you still have problems I would look into and debug the source code of: wp_insert_post()
if ( ! empty( $postarr['page_template'] ) && 'page' == $data['post_type'] ) {
$post->page_template = $postarr['page_template'];
$page_templates = wp_get_theme()->get_page_templates( $post );
if ( 'default' != $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) ) {
if ( $wp_error ) {
return new WP_Error('invalid_page_template', __('The page template is invalid.'));
}
update_post_meta( $post_ID, '_wp_page_template', 'default' );
} else {
update_post_meta( $post_ID, '_wp_page_template', $postarr['page_template'] );
}
}
So its probably this part that fails:
if ( 'default' != $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) )
Try to modify: wp-includes/post.php and go to the definition of: function wp_insert_post() on row: 2872. And add a new row on row: 3312 for debugging purposes.
echo '<pre>';
print_r( $page_templates );
echo '</pre>';
die();
Make sure your certificate.php is among those in that array. Remember to delete the debug code before continuing. This should give you some answers.

Template Overriding using a customization section

I am trying to override my default templates from customization section, I am using code to do that, but if I am using it I am unable to assign a template to the edit-page page, Can anyone give an idea how both the customization section and edit-page assign template work. I want to set the template when I am creating a page and after assigning it I want to override.
Consider I have a blog page, I want to assign it archive.php template and ten want to override It from customization section. There is the particular condition where I want it to work.
<?php
/**
* Adds the Customize page to Select template For Pages
*/
add_action( 'wp_footer', 'cur_page_template' );
function cur_page_template(){
var_dump( get_option('current_page_template') );
var_dump( get_page_template() );
exit;
}
function widgetsite_template_override($wp_customize){
$wp_customize->add_panel( 'template_options', array(
'title' => __( 'Template Options', 'widgetsite' ),
'description' => $description, // Include html tags such as <p>.
'priority' => 160, // Mixed with top-level-section hierarchy.
) );
$wp_customize->add_section('theme_template_override', array(
'title' => __('Override Templates', 'widgetsite'),
'panel' => 'template_options',
'description' => '',
'priority' => 120,
));
$templates = get_page_templates();
$cats = array();
$i = 0;
foreach($templates as $template_name => $template_file){
//$cats[$template_name] = $template_name;
if (strpos($template_file,'layouts') !== false) {
$cats[$template_file] = $template_name;
}
}
$wp_customize->add_setting('widgetsite_archive_template');
$wp_customize->add_setting('widgetsite_page_template');
$wp_customize->add_setting('widgetsite_index_template');
$wp_customize->add_setting('widgetsite_post_template');
$wp_customize->add_setting('widgetsite_search_template');
$wp_customize->add_control( 'widgetsite_archive_template', array(
'settings' => 'widgetsite_archive_template',
'label' => 'Override Archive Template:',
'section' => 'theme_template_override',
'type' => 'select',
'choices' => array_merge(array( "archive.php"=>get_option('current_page_template')), $cats)
));
$wp_customize->add_control( 'widgetsite_page_template', array(
'settings' => 'widgetsite_page_template',
'label' => 'Override Page Template:',
'section' => 'theme_template_override',
'type' => 'select',
'choices' => array_merge( array( "page.php" =>get_option('current_page_template')), $cats)
));
$wp_customize->add_control( 'widgetsite_index_template', array(
'settings' => 'widgetsite_index_template',
'label' => 'Override Index Template:',
'section' => 'theme_template_override',
'type' => 'select',
'choices' => array_merge(array( "index.php"=>get_option('current_page_template')), $cats)
));
$wp_customize->add_control( 'widgetsite_post_template', array(
'settings' => 'widgetsite_post_template',
'label' => 'Override Post Template:',
'section' => 'theme_template_override',
'type' => 'select',
'choices' => array_merge(array( "post.php"=>get_option('current_page_template')), $cats)
));
$wp_customize->add_control( 'widgetsite_search_template', array(
'settings' => 'widgetsite_search_template',
'label' => 'Override Search Template:',
'section' => 'theme_template_override',
'type' => 'select',
'choices' => array_merge(array( "search.php"=>get_option('current_page_template')), $cats)
));
}
add_action('customize_register', 'widgetsite_template_override');
$theme_mode_templates['archive.php'] = get_theme_mod("widgetsite_archive_template");
$theme_mode_templates['page.php'] = get_theme_mod("widgetsite_page_template");
$theme_mode_templates['index.php'] = get_theme_mod("widgetsite_index_template");
$theme_mode_templates['post.php'] = get_theme_mod("widgetsite_post_template");
$theme_mode_templates['search.php'] = get_theme_mod("widgetsite_search_template");
function widgetsite_template_redirect($template){
global $wp_query;
global $post;
$cur= basename($template);
if( $cur === 'page.php' && get_theme_mod("widgetsite_page_template")){ //note $cur will never be empty!
$template= get_template_directory() . '/' . get_theme_mod("widgetsite_page_template");// assuming this will return correct template...
//if issues try hardcoding a path to test...
}
if( $cur === 'archive.php' && get_theme_mod("widgetsite_archive_template")){ //note $cur will never be empty!
$template= get_template_directory() . '/' . get_theme_mod("widgetsite_archive_template");// assuming this will return correct template...
//if issues try hardcoding a path to test...
}
if( $cur === 'index.php' && get_theme_mod("widgetsite_index_template")){ //note $cur will never be empty!
$template= get_template_directory() . '/' . get_theme_mod("widgetsite_index_template");// assuming this will return correct template...
//if issues try hardcoding a path to test...
}
if( $cur === 'post.php' && get_theme_mod("widgetsite_post_template")){ //note $cur will never be empty!
$template= get_template_directory() . '/' . get_theme_mod("widgetsite_post_template");// assuming this will return correct template...
//if issues try hardcoding a path to test...
}
if( $cur === 'search.php' && get_theme_mod("widgetsite_search_template")){ //note $cur will never be empty!
$template= get_template_directory() . '/' . get_theme_mod("widgetsite_search_template");// assuming this will return correct template...
//if issues try hardcoding a path to test...
}
return $template;
}
add_filter( 'template_include', 'widgetsite_template_redirect', 99 );
How the choose template box works from post edit screen.
It is important to remember pages are also posts and all meta relating to posts are stored in the post meta table. Page post types differ slightly from the standard post types as they do not follow the single-postname.php template use function. Instead pages save the template file path in the wp_postmeta database table with a key of _wp_page_template.
So one option to change this value is to change it after save post.
function save_template_file( $post_id ) {
if ( 'page' != $post->post_type ) {
return;
}
//insert logic here
$filelocation= 'anywhere.....';
update_post_meta($post_id, '_wp_page_template', $filelocation);
}
add_action('save_post', 'save_template_file', 11 );
Now this is not what you are looking for, but you mentioned you wanted to understand the process, so for pages, wp will reference the template file from post meta and pull this value. So you can change it after saving if it will always follow the same logic (slightly optimized the process). This is the file that shows up in the edit post screen and will always pull the db value unless wp tries to load the template and realizes it does not exist anymore, in which case it reverts to the defaults file in the select box.
The filter template_include is within the function that searches for the correct template for the pages post type (other post types have the filter single_template)
Your use of include here is incorrect. Don't forget a filter will expect a value returned to work correctly in this case $template.
So if we want to change the template for pages....
add_filter('template_include', 'assign_new_template');
function assign_new_template ($template){
//we already have a template name, no need to pull it again..
$cur= basename($template);
if( $cur === 'page.php' && get_theme_mod("widgetsite_page_template")){ //note $cur will never be empty!
$template= get_template_directory() . '/layouts/' . get_theme_mod("widgetsite_page_template");// assuming this will return correct template...
//if issues try hardcoding a path to test...
}
// dont need a else, we will only change the template if our logic is satisfied...
// etc
return $template;
}
Setting the selects
You are missing the value for default so i propose the following mod as i cant see what your setting in get_option('current_page_template') but if there is a correct filename there replace page.php with it.
While you are not setting a default value for your select box, your page will render the 1st value of the select if none are marked selected so it should work the same.
$wp_customize->add_control( 'widgetsite_search_template', array(
'settings' => 'widgetsite_search_template',
'label' => 'Override Search Template:',
'section' => 'theme_template_override',
'type' => 'select',
'choices' => array_merge(array("page.php"=>'default'), $cats)
));
If you resave all the options like above it should be working (it was for me)!

NinjaForm - How To Search & Retrieve By DateTime?

I'm using NinjaForm plugin on wordpress. Here how to search and retrieve data:
<?php
$args = array(
'form_id' => $form_id,
'user_id' => $user_id,
'fields' => array(
'34' => 'checked',
'54' => 'Hello World',
),
);
// This will return an array of sub objects.
$subs = Ninja_Forms()->subs()->get( $args );
// This is a basic example of how to interact with the returned objects.
// See other documentation for all the methods and properties of the submission object.
foreach ( $subs as $sub ) {
$form_id = $sub->form_id;
$user_id = $sub->user_id;
// Returns an array of [field_id] => [user_value] pairs
$all_fields = $sub->get_all_fields();
// Echoes out the submitted value for a field
echo $sub->get_field( 34 );
}
What I want to do is searching by DateTime fields. How do I do that?
I have tried change args like this but result same.
$args = array(
'form_id' => 5,
'date_modified'=> '2015-07-25 3:19:09'
);
or like this
$args = array(
'form_id' => 5,
'date_modified'=> '< 2015-07-25 3:19:09'
);
Did I do wrong?
Find Ninja DB Table:
Go into your database using phpmyadmin or something and find the table Ninja Forms is using. Hopefully they're using their own table. If not, you can search each wp table for some of the arg data that you know returns a form from Ninja_Forms(). Or go into the Ninja plugin code and try and find where they interact with the db to find which table they write into.
Write your own mysql search code:
Instead of using Ninja's class to search, use wordpress's built in mysql search and throw in the table you found in step 1.
GLOBAL $wpdb;
$wpdb->get_results($wpdb->prepare("SELECT * FROM `ninja_table` WHERE `date_modified` = %s", $strDate));
I haven't tested, but this would be my course of action.
Use begin_date and end_data parameters to get the submissions
$args = array(
'form_id' => $form_id,
'begin_date' => '2015-07-20 0:00:00',
'end_date' => '2015-07-25 3:19:09'
);
$subs = Ninja_Forms()->subs()->get( $args );

How do you target a specific page in Wordpress functions.php?

I am currently using the Multi Post Thumbnails plugin for Wordpress, but I only want the extra thumbnails provided by the plugin to show on one specific page. The plugin does not appear to natively support this functionality but it seems like something that would be pretty easy to add, I'm just not sure of the right way to go about it as I'm fairly new to Wordpress development.
The code for Multi Post Thumbnails is the following, which simply goes in functions.php:
if (class_exists('MultiPostThumbnails')) {
new MultiPostThumbnails(
array(
'label' => 'Secondary Image',
'id' => 'secondary-image',
'post_type' => 'page'
)
);
new MultiPostThumbnails(
array(
'label' => 'Tertiary Image',
'id' => 'tertiary-image',
'post_type' => 'page'
)
);
}
It seems to me it would just be a simple case of wrapping this in a check so that it only runs for a specific page ID, but I'm not quite sure how to go about doing that.
This is probably somewhat of a hack. To my knowledge post/page id's are not accessible from inside functions.php.
// get the id of the post/page based on the request uri.
$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$post_id = url_to_postid($url);
// the id of the specific page/post.
$specific_post_id = 3;
// check if the requested post id is identical to the specific post id.
if ($post_id == $specific_post_id) {
if (class_exists('MultiPostThumbnails')) {
new MultiPostThumbnails(
array(
'label' => 'Secondary Image',
'id' => 'secondary-image',
'post_type' => 'page'
)
);
new MultiPostThumbnails(
array(
'label' => 'Tertiary Image',
'id' => 'tertiary-image',
'post_type' => 'page'
)
);
}
}
This is also probably a hack but it worked for me. I got stung by the AJAX 'post_id' back to the admin page once the image has been selected. My usage was for a slug but the function could easily be modified for a post ID.
function is_admin_edit_page( $slug ){
if( ( isset($_GET) && isset($_GET['post']) ) || ( isset($_POST) && isset($_POST['post_id']) ) )
{
$post_id = 0;
if(isset($_GET) && isset($_GET['post']))
{
$post_id = $_GET['post'];
}
else if(isset($_POST) && isset($_POST['post_id']))
{
$post_id = $_POST['post_id'];
}
if($post_id != 0)
{
$c_post = get_post($post_id);
if( $c_post->post_name == $slug )
{
return true;
}
}
}
return false;
}
if( is_admin_edit_page('work') ) {
new MultiPostThumbnails(
array(
'label' => 'Hero 1 (2048px x 756px JPEG)',
'id' => 'am-hero-1',
'post_type' => 'page'
)
);
}

Resources