Wordpress Plugin HTML Bootstrap + PHP - php

I'm creating my first wordpress plugin to show some dashboard data on the wp-admin-page, however only my php code is recognized when I edited my plugin, any html markup I write is omitted.
I want to make my plugin using HTML+bootstrap for the Front-end but make some calculations using PHP but I don't understand how I can do this.
In my first attempt, I could only echo html markup:
<?php
add_action( 'admin_menu', 'test_plugin_setup_menu' );
function test_plugin_setup_menu(){
add_menu_page( 'Page Title', 'Menu title', 'manage_options', 'test-plugin', 'test_init' );
}
function test_init(){
echo "<h1> Hello World! </h1>";
}
?>/*THIS OUTPUT ON MY WP ADMIN PAGE AS EXPECTED WITH A HELLO WORLD
However I need to write a lot of HTML markup with Bootstrap and only use PHP in some areas but any HTML code I write after my ?> doesn't get show on the page.
Second approach: I referred to my HTML file with PHP
<?php
add_action( 'admin_menu', 'test_plugin_setup_menu' );
function test_plugin_setup_menu(){
add_menu_page( 'Page Title', 'Menu title', 'manage_options', 'test-plugin', 'test_init' );
}
function test_init(){
$file = file_get_contents('index.html', FILE_USE_INCLUDE_PATH);
if($file == false)
{
echo 'file not found';
}
else
{
echo $file;
}
}
?>
This last attempt indeed shown all my HTML login with my bootstrap styled perfectly on the wp-admin-page but i cant use the PHP logic or DB connections I need on this HTML file..
My question is: how can I use my HTML markup in my PHP plugin simultaneously?
Extra info
I'm very new to PHP and Wordpress so I don't know what am I doing wrong in my code so HTML tags cant be show on the page unless I referred them to an external file.
Any help is greatly appreciated.

Another alternative would be to include your php file. Here's the idea:
add_action( 'admin_menu', 'test_plugin_setup_menu' );
function test_plugin_setup_menu() {
add_menu_page( 'Page Title', 'Menu title', 'manage_options', 'test-plugin', 'test_init' );
}
function test_init() {
ob_start();
include_once 'file-that-i-want-to-include.php';
echo ob_get_clean();
}
Then of course in your file-that-i-want-to-include.php. Just create your page like you normally do:
<?php
include_once 'mydatabaseconnction.php';
// ... and so on php codes of sorts
?>
<div class="container">
<div class="row">
<div class="col">
my contents whatever
</div>
</div>
</div>
In addition, you can just load bootstrap in that specific admin settings page if you'd like:
add_action('admin_enqueue_scripts', function() {
if (get_current_screen()->id === 'toplevel_page_test-plugin') { // the prefix is "toplevel_page_"
$css_path = plugins_url('assets/vendor/bootstrap/bootstrap.min.css'); // just change the path going to your css and js
$js_path = plugins_url('assets/vendor/bootstrap/bootstrap.min.js');
wp_enqueue_style('latest-bootstrap-css', $css_path, [], '4.5.0');
wp_enqueue_script('latest-bootstrap-js', $js_path, [], '4.5.0');
}
});

Related

do_settings_sections() is not working with WordPress Plugins PHP

I am new to both Wordpress development and PHP and I am currently following this tutorial that I really enjoy: https://www.youtube.com/watch?v=hbJiwm5YL5Q
But it did not take long until my code (even though eyactly the same as in the tutorial to my findings) stopped behaving as expected. The do_settings_sections doesn´t render the field.
What am I doing wrong? Unlike in this question I initialized the function, didn´t I?
My code:
<?php
/*
Plugin Name: Word Count Statistics
Description: A plugin to display the word and character count, as well as the aproximate reading time for an article. Where and what to display can be customized on the settings page.
Version: 1.0
...
*/
//using a class allows the usage of function names, that don´t need to be unique
class WordCountAndTimePlugin
{
function __construct()
{
//array allows referencing the function names in the class
add_action('admin_menu', array($this, 'adminPage'));
add_action('admin-init', array($this, 'settings'));
}
function adminPage()
{
add_options_page(
'Word Count Settings', //Title of page
'Word Count', //Display name in menu
'manage_options', //Access allowed if user may...
'word-count-settings-page', //Slug name
array($this, 'displayHTML') //Function to call
);
}
function displayHTML()
{
?>
<div class="wrap">
<h1>Word Count Settings</h1>
<form action="options.php" method="POST">
<?php
do_settings_sections('word-count-settings-page');
submit_button();
?>
</form>
</div>
<?php
}
function settings()
{
//Creates a form section
add_settings_section(
'wcp_first_section', //Section name
'some text here', //Subtitle
null, //Content / Desription
'word-count-settings-page' //Slug name
);
//Creates a setting field
add_settings_field(
'wcp_location', //Name of setting
'Display Location', //HTML label text
array($this, 'displayLocationHTML'), //Function to display custom HTML
'word-count-settings-page', //Slug name
'wcp_first_section' //Section name
);
//Registers the setting field to the WordPress settings
register_setting(
'wordcountplugin', //Group it belongs to
'wcp_location', //Name of setting
array(
'sanitize_callback' => 'sanitize_text_field', //Validates value
'default' => '0' //Default value
) //Array of options
);
}
function displayLocationHTML()
{
?>
Hello
<?php
}
}
$wordCountAndTimePlugin = new WordCountAndTimePlugin();
?>
Thanks in advance :)
You have a small mistake in the hook`s name. You need to replace:
add_action('admin-init', array($this, 'settings'));
on:
add_action('admin_init', array($this, 'settings'));

Wordpress shortcode return another php page

My custom wordpress plugin shortcode is working when I just return something. But I want to return another php page, which includes html.
This is my map.php
<?php
function example($map){
echo "Hello!";
}
?>
I've tried like this
add_shortcode( 'map_shortcode', 'map_shortcode' );
function map_shortcode() {
wp_enqueue_style( 'my-css' );
wp_enqueue_style( 'meyer-reset' );
wp_enqueue_style( 'tooltipster-bundle' );
wp_enqueue_script( 'mypluginscript' );
return $map;}
?>
I mean the shortcode works; when I tried just a simple string like 'abc' it showed, so it works, but I want to show the other page on the return function.
To show an HTML view using WordPress shortcode, you can use ob_start() and ob_get_clean() functions.
https://www.php.net/manual/en/function.ob-start.php
https://www.php.net/manual/en/function.ob-get-clean.php
<?php
function map_shortcode() {
ob_start();
?>
<!-- Your HTML codes here ... -->
<?php
return ob_get_clean();
}
add_shortcode("map_shortcode", "map_shortcode");
?>

create new page within my admin page wordpress

I'm creating a new plug for my project I have the like in the admin menu and i have created the landing page I would like to know how i can create a new page within my plugin.
So if I was to click on View All Images how could i navigate to a new page within my plugin?
add_action('admin_menu', 'test_plugin_setup_menu');
add_action('admin_enqueue_scripts', 'my_admin_scripts');
function test_plugin_setup_menu(){
add_menu_page( 'Test Plugin Page', 'Test Plugin', 'manage_options', 'test-plugin', 'test_init' );
}
function test_init(){
echo "<a href='#'>View All Images</a>";
}
Use plugins_url() function
function test_init(){
echo "<a href='<?=plugins_url();?>/your_file.php'>View All Images</a>";
}

Magnific Popup with Wordpress Galleries

I am building a Wordpress theme and I want to add Magnific Popup - dimsemenov.com/plugins/magnific-popup/
I managed to get it working with single images with the following code in my functions.php file:
/**
* Add title back to images
*/
function pexeto_add_title_to_attachment( $markup, $id ){
$att = get_post( $id );
return str_replace('<a ', '<a title="'.$att->post_title.'" ', $markup);
}
add_filter('wp_get_attachment_link', 'pexeto_add_title_to_attachment', 10, 5);
add_action('wp_enqueue_scripts', 'enqueue_magnificpopup_styles');
function enqueue_magnificpopup_styles() {
wp_register_style('magnific_popup_style', get_stylesheet_directory_uri().'/js/magnific-popup.css', array('style.css'));
wp_enqueue_style('magnific_popup_style');
}
add_action('wp_enqueue_scripts', 'enqueue_magnificpopup_scripts');
function enqueue_magnificpopup_scripts() {
wp_register_script('magnific_popup_script', get_stylesheet_directory_uri().'/js/jquery.magnific-popup.min.js', array('jquery'));
wp_enqueue_script('magnific_popup_script');
wp_register_script('magnific_init_script', get_stylesheet_directory_uri().'/js/jquery.magnific-popup-init.js', array('jquery'));
wp_enqueue_script('magnific_init_script');
}
However it says 'Image could not be loaded' when I click an image in a Wordpress Gallery as it obviously has different HTML markup. Has anyone found a way to make this work with Wordpress Galleries? Thanks.
Something like this should work if the images are linked to "image file". Put this in a javascript file and enqueue the file after jQuery is ready.
jQuery(document).ready(function($) {
$(".gallery-icon a").magnificPopup();
});

Targeting a specific `<div>` via Wordpress Plugin

Insert HTML Input to a themes template file?
My question is this:
Currently with my code, I can have whatever I've set in my custom settings page appear on
the end of every post using, of course, a WordPress filter.
The only thing is, I don't want what I input to go anywhere within a post. What I am trying to
achieve, is to have my input injected into the current themes home.php template file, within a <div></div> tag that's contained within that template file. The <div> in question has an ID attached, which is <div id="category-inner">, so is there any way use it's ID to target it in my plugin?
I've successfully managed to do it, by editing the actual home.php template file and inserting a bit of php directly there to show the user input, but that totally goes against what I'm trying to do, which is to not have to edit the source code of the template file and only have to use my plugin to insert the users inputted text in that specific location (the <div> mentioned above).
My plugin is only ever going to be adding user input in one place on the site, and it will never change. And the place it will always go in, is where I mentioned above.
Below is my plugin code:
<?php
/*/
Plugin Name: Custom Text Adder
Plugin URI: N/A
Description: Demonstrates how rubbish I am at pretty much everything I want to do
Version: 101
Author: JD
Author URI: N/A
/*/
// insert custom plugin settings menu
add_action('admin_menu', 'custom_create_menu');
add_filter('the_content', 'customhead_the_content');
function customhead_the_content($content) {
$output = $content;
$output .= '<div id="category-inner">';
$output .= get_option('post_text');
$output .= '</div>';
return $output;
}
// Add Font-Size to WYSIWYG Editor
function wp_editor_fontsize_filter( $options ) {
array_shift( $options );
array_unshift( $options, 'fontsizeselect');
array_unshift( $options, 'formatselect');
return $options;
}
add_filter('mce_buttons_2', 'wp_editor_fontsize_filter');
// Create Custom Menu
function custom_create_menu() {
//create new top-level menu
add_menu_page('Custom Plugin Settings', 'Custom Settings', 'administrator', __FILE__, 'custom_settings_page',plugins_url('/img/icon.png', __FILE__));
//call register settings function
add_action( 'admin_init', 'register_mysettings' );
}
// Register our settings
function register_mysettings() {
register_setting( 'custom-settings-group', 'new_option_name' );
register_setting( 'custom-settings-group', 'some_other_option' );
register_setting( 'custom-settings-group', 'option_etc' );
register_setting( 'custom-settings-group', 'font_size' );
register_setting( 'custom-settings-group', 'post_text' );
}
function custom_settings_page() {
?>
<!-- Custom Settings Page Container -->
<div class="wrap">
<h2>Custom Text</h2>
<form method="post" action="options.php">
<?php settings_fields( 'custom-settings-group' ); ?>
<table class="form-table">
<?php /* Bring the editor onto the page */
wp_editor( '', 'post_text', $settings = array() );
// 4.
// Custom buttons for the editor.
// This is a list separated with a comma after each feature
// eg. link, unlink, bold, ...
$settings = array(
'textarea_name' => 'post_text',
'media_buttons' => true,
'tinymce' => array(
'theme_advanced_buttons1' => 'formatselect,|,bold,italic,underline,|,' .
'bullist,blockquote,|,justifyleft,justifycenter' .
',justifyright,justifyfull,|,link,unlink,|' .
',spellchecker,wp_fullscreen,wp_adv'
)
);
submit_button( 'Save everything', 'primary', 'submit' ); ?>
</table>
</form>
</div>
<?php }; ?>
Here is the screenshot which will be able to explain this in the most concise way possible:
http://i.imgur.com/hiEjEsA.jpg
Again, any and all help is hugely appreciated and will hopefully stop my brain from hurting!
You all rock,
Casey. :)
I'm not sure if you want this to be a pure PHP solution, but since you have the ID of the div you could target it with jQuery and insert the text into the div on page load (or form submit) with something like this:
$(document).ready(function() {
var userText = $('#input-id').val();
$('#category-inner').html(userText);
})

Categories