WordPress add_setting_section does not recognise custom page - wordpress

I trying to create a custom settings page for my plugin, but I'm unable to make the page display a custom settings page, to make things simple I just want to display the section header.
Here is what I have.
private function add_hooks() {
add_action('admin_menu', array($this,'register_menu'));
add_action("admin_init", array($this,"display_options"));
}
public function register_menu() {
add_menu_page('Feed','API FEED','manage_options','adt-feed',array($this,'include_admin_page'),'dashicons-format-image');
}
public function include_admin_page() {
return include_once( PLUGIN_PATH 'admin/view/config-page.php' );
}
public function display_options()
{
//section name, display name, callback to print description of section, page to which section is attached.
add_settings_section("adt_general_section", "Header Options", array($this, "display_header_options_content"), "adt-feed");
}
public function display_header_options_content() { //THIS NEVER GETS CALLED
echo "PLUGIN HEADER SECTION";
}
and them in my config-page.php i have:
<div class="wrap">
<div id="icon-options-general" class="icon32"></div>
<h1>ADT Feed Options</h1>
<form method="post" action="options.php">
<?php
//add_settings_section callback is displayed here. For every new section we need to call settings_fields.
settings_fields("adt_general_section");
// Add the submit button to serialize the options
submit_button();
?>
</form>
</div>
Using some "die()" calls i manage to find that display_header_options_content never gets called, if i change "adt-feed" for "general" in the add_settings_section, i can see the message: "PLUGIN HEADER SECTION" in the General settings page.
thanks in advance for any help!

You need to call
do_settings_sections( 'adt-feed' );
in config-page.php.
A good place to put this is just before the call to submit_button();

Related

How elaborate input of a WordPress shortcode form ($_POST array)?

I'm trying to create a custom WordPress shortcode to generate a form.
I wrote a function ( called send_advice_request_form() ) that returns HTML form code, then the callback that use ob_start() and return ob_get_clean(), how can I elaborate submitted form data? If I don't indicate any action attribute for form, where the post data will be submitted?
function send_advice_request_form_cb() {
ob_start();
echo send_advice_request_form();
return ob_get_clean();
}
add_shortcode( 'dparequestform', 'send_advice_request_form_cb' );
function send_advice_request_form() { //return HTML form }
The best way for handling custom form like yours is to send the datas to admin-post.php
Create a form with this action and this input type hidden :
<form action="<?php echo esc_url(admin_url('admin-post.php')); ?>">
/*
Your fields here.
*/
<input type="hidden" name="action" value="your_action_name">
</form>
Then you will have to handle datas with those hooks
function send_advice_request_handler() {
/*
Do what you have to do.
*/
}
// Hook for everyone, no private
add_action( 'admin_post_nopriv_your_action_name', 'send_advice_request_handler' );
// Hook only for logged in users (verified by wordpress with is_user_logged_in())
add_action( 'admin_post_contact_form', 'send_advice_request_handler' );
I hove it will helps you, tell me if you need more help.

Add a custom button in WordPress register form

I am trying to add a button in wordpress register form but the position of button is not right as i want. It is looking like the below image
But i want it something like this
Given below is my code
public function init()
{
global $mySetting;
$mySetting = get_option('my_options');
add_action('register_form', [$this, 'my_registration_button']);
add_action('login_form', [$this, 'my_registration_button']);
}
public function my_registration_button()
{
?>
<p>My Button</p>
<?php
}

Wordpress: Add content to edit.php

I'm trying to find out what action hook/filter I can use to insert content on the admin "edit.php" page (i want to place a few links above the 'posts' table)? I've found "edit_form_after_title" and "edit_form_after_editor" (these do exactly what I want to do, but they are for posts.php, not edit.php).
With the help of this answer: How do you create a custom edit.php / edit pages page
I came up with this:
<?php
# Called only in /wp-admin/edit.php pages
add_action( 'load-edit.php', function() {
add_filter( 'views_edit-talk', 'talk_tabs' ); // talk is my custom post type
});
# echo the tabs
function talk_tabs() {
echo '
<h2 class="nav-tab-wrapper">
<a class="nav-tab" href="admin.php?page=guests">Profiles</a>
<a class="nav-tab nav-tab-active" href="edit.php?post_type=talk">Talks</a>
<a class="nav-tab" href="edit.php?post_type=offer">Offers</a>
</h2>
';
}
?>
And it looks like this:
If you just wanted to add to the post title link you could do something like this
if (is_admin()) {
add_filter('the_title', function($title) {
return $before_title . $title . $after_title;
});
}
however, it doesn't sound like you want to add text to the title link.
To add html after the title and before the actions links, you could do like this
if (is_admin()) {
add_filter('post_row_actions', function($args) {
// echo your custom content here
return $args; // and dont forget to return the actions
});
}
There is also page_row_actions for the page edit screen (post_row_actions is only for posts)
As far as adding stuff before the title, I don't see a hook/filter to do that. See wp-admin/class-wp-posts-list-table.php line 463 function single_row if you want to look for yourself.

How to add a button to a custom post type in Wordpress?

I have a "Products" custom post type. Normally, this custom post type have an "Add New" button. I want to add another button call "Update from Provider".
Currently, I have modify the Wordpress code (in "wordpress\wp-admin\includes\class-wp-list-table.php") to add that button. In this case, when I update Wordpress, my modified code will be deleted. Therefore, I need to move that button to my plug-in code.
In this case, please help me how to move that button to my plug-in code.
Well, if you opened the core file you saw that there's no action in it where we can hook.
Only a couple of filters. We can use the following:
add_filter( 'views_edit-movies', 'so_13813805_add_button_to_views' );
function so_13813805_add_button_to_views( $views )
{
$views['my-button'] = '<button id="update-from-provider" type="button" title="Update from Provider" style="margin:5px">Update from Provider</button>';
return $views;
}
It produces this:
To put it in an approximate position from where you'd like, use the following:
add_action( 'admin_head-edit.php', 'so_13813805_move_custom_button' );
function so_13813805_move_custom_button( )
{
global $current_screen;
// Not our post type, exit earlier
if( 'movies' != $current_screen->post_type )
return;
?>
<script type="text/javascript">
jQuery(document).ready( function($)
{
$('#update-from-provider').prependTo('span.displaying-num');
});
</script>
<?php
}
Which results in this:

Submit To PDF in WordPress

Is there a plugin or something that creates PDF files from a entered by the user form data, when it clicks on the submit button?
if youre wanting a form filled in, then the details of that form processed and sent to you via email with a pdf attachment of the details, i had something similar a few weeks back, I couldnt find anything that would work the way i wanted, so....
I setup my form then using a custom page template I assigned that to a page, then using php and the html2pdf class i created my pdf which was emailed as an attachment...
heres the code i used..
been minified for this page (remember to sanitize your user input).
<?php
/*
Template Name: FORMTOPDF
*/
?>
<?php get_header(); ?>
<style>
/* STYLES FOR ERROR PLACEMENT */
label {
width: 80px;
text-align: right;
float: left;
}
.formerror {
border: 1px solid red;
background-color : #FFCCCC;
width: auto;
padding: 5px 0;
padding-left:10px;
}
.errortext {
font: bold smaller sans-serif;
}
</style>
<?php
// CREATE AN ARRAY FOR OUR ERRORS
$arrErrors = array();
// Check for FORM SUBMISSION
// using hidden form field
if(isset($_POST['action']) && ($_POST['action']=='send'))
{
/* ================= START FORM DATA ========================= */
$name = trim($_POST['name']);
if ($name=='') $arrErrors['name'] = 'Please provide your name.';
$email = trim($_POST['email']);
if ($email=='') $arrErrors['Email'] = 'Please provide your Email Address.';
$comments = trim($_POST['your-comments']);
if ($comments=='') $arrErrors['Comments'] = 'Please add your Comments.';
/* ================= END FORM DATA ========================= */
if (count($arrErrors) == 0) {
// Process form here
/* ================= START PDF CREATION ========================= */
$strContent = "<p>Submission from ".$name."</p>";
$strContent.= "<p><strong>Name</strong>:".$name."</p>";
$strContent.= "<p><strong>Email </strong>: ".$email."</p>";
$strContent.= "<p><strong>Comments</strong> : <br />".$comments."</p>";
/* ================= END PDF CREATION ========================= */
// Include our HTML to PDF creator
// FROM THEME DIRECTORY?
require(TEMPLATEPATH.'/html2pdf/html2fpdf.php');
$pdf=new HTML2FPDF();
$pdf->AddPage();
// folder location of HTML file
$fileLocation = "wp-content/uploads/";
// Call to the file name from the URL
$fileName = "Form_Submission_From_".$name;
// add the location 'wp-content/uploads/' to the fileToOpen
$fileToOpen = $fileLocation;
// Then add the actual file name // form_submission.pdf
// output should look like 'wp-content/uploads/form_submission_from_(name).pdf'
$fileToOpen .= $fileName.".pdf";
// Open the file with read access
$fp = fopen($fileToOpen,"r");
//$strContent = fread($fp, filesize($fileToOpen));
// Close of the page
fclose($fp);
// Create new PDF document from the Content
$pdf->WriteHTML($strContent);
// create our PDF in the wp uploads folder
$pdf->Output("wp-content/uploads/" .$fileName. ".pdf");
/* ================= END PDF ========================= */
/* ================= START EMAIL ========================= */
$headers= "From: YourWebsite <info#yourwebsite.co.uk>\r\n\\";
$emailSubject = "Submission from " . $name;
$emailAdmin = "admin#yourwebsite.co.uk";
$emailMessage = "Submission from ".$yourcompanyname."\n\n";
$emailMessage.= "Company Name: ".$yourcompanyname."\n";
$emailMessage.= "Email : ".$email."\n";
$emailMessage.= "Comments : \n".$comments."\n\n";
$attachments = array(WP_CONTENT_DIR ."/uploads/".$fileName.".pdf", $target_path);
wp_mail($emailAdmin, $emailSubject, $emailMessage, $headers, $attachments);
// Delete our PDF from the server after email Sent
// uncomment this to delete after email sent?
//unlink($fileToOpen);
/* ================= END EMAIL ========================= */
// show thank you message if successful
$strGood = '<div class="formerror" style="background:#FFC;">
<h2>Thank You</h2>
<p>Thank you for contacting us.</p>
</div>';
}else{
// The error array had something in it. There was an error.
// Start adding error text to an error string.
$strError = '<div class="formerror"><p><img style="margin-left:10px;" src="'.get_option('home').'/wp-content/themes/mytheme/media/images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><strong>Please check the following and try again:</strong></p><ul style="margin-left:50px;">';
// Get each error and add it to the error string
// as a list item.
foreach ($arrErrors as $error) {
$strError .= "<li style='list-style-type:circle;'><em>$error</em></li>";
}
$strError .= '</ul></div><br />';
}
}// NOT BEEN SUBMITTED
// show regular page with form
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2>
<?php the_title(); ?>
</h2>
<?php
// show errors if there is any
echo $strError;
?>
<?php
// show thank you if successful
echo $strGood;
?>
<?php the_content('<p>Read the rest of this page ยป</p>'); ?>
<form method="post" action="<?php bloginfo('url');?>/your-form-page/" enctype="multipart/form-data">
<input type="hidden" value="send" name="action">
<p <?php if (!empty($arrErrors['name'])) echo ' class="formerror"'; ?>>Your Name:
<span class="errortext" style="color:#F00;">(required)</span><br>
The rest of the form below here ------ >
</form>
<?php endwhile; endif; ?>
<?php get_footer(); ?>
thats it, once the user fills out the form, (my form had a lot more fields than this plus an upload field for files to be attached also.)
but the forms submitted, checks for required fields, if successful it will create a pdf file from the $strContent variable, then attaches this to the email to be sent using the wp_mail from wordpress.. then displays a thank you message, or else it will show and highlight any errors,
hope this helps..
I found these plugins that allow posts to be emailed or downloaded as pdf's.
http://wordpress.org/extend/plugins/tags/create-pdf
If you are flexible, It seems possible to programmatically create posts from your form submitted by a user, then create a pdf of that post. The posts that are created from the form could easily be assigned a particular category which is not displayed on the site.
To programmatically create, update, and delete posts, see the WordPress Function Reference, and in particular:
wp_insert_post
wp update post
wp delete post
A quick google search exhibits plenty of ways to create pdf's with php. Some hard, some less hard. I found this class that might get you started: "FPDF"
There is a plugin extension called Gravity PDF, that extends from Gravity Forms. It will generate a PDF from a form, and you can choose to download or email it.
Source
https://wordpress.org/plugins/gravity-forms-pdf-extended/
I haven't done an exhaustive search of the WP-plugins, but from as far as I can tell the answer is no. Of course, it would be possible to create such a plugin from scratch however the server hosting WP would need to have the proper libraries installed in order for the plugin to be useful.
We created a custom solution for one of our clients to get this done since there weren't any ready available plugins. This system creates pdf/word documents by fetching data from Gravity Forms.
You can check out details of the solution here. Gravity Forms to PDF/Word Document Auto-Fill Soluion

Resources