wordpress custom page not in admin - wordpress

I am trying to create a page test.php that can use wordpress functions but is not part of the admin/pages section. so basically if I have a link on my single.php or page.php to direct me to test.php not show the 404 but actually show me the contents of test.php. I've heard that i might have to make some changes to the htaccess but I'm not sure how to search for this issue so any help is greatly appreciated.

You can use include('../wp-load.php'); in the top of your php file to get wordpress functionality in to your file then place your file in the root wordpress installation and then you can call your file with your url http://yourdomain.net/test.php
The information for creating page templates, plugins and using functions.php can all be found on the WordPress site.
How to call wordpress functions in custom php script

if ( is_admin() ) {
add_action( 'admin_menu', array( 'adminAddPage' ) );
}
/**
* Callback to add page
*/
public function adminAddPage() {
add_options_page( 'Custom page title', 'Custom page title', 'manage_options', 'custom_page_slug', array('adminPage') );
}
/**
* Page HTML Callback
*/
public function adminPage() {
// your html code
}

Related

Custom template for one specific post in a custom post type in wordpress

I have a little web app, that I want to run within a page in a custom post type on my WordPress site.
So I thought to just create a new template to put the specific code for this app there and assign this template to the custom post types page.
Sadly Wordpress is not showing me the template in my cpt.
The template file is called fretfind.php and the first lines contain:
/*
Template Name: Fretfind
Template Post Type: guitar
*/
'guitar' is the cpt slug.
Has anyone an idea why it doesn't show me the selecting option for this template. Are there any extra steps for Custom Post Type Templates?
Or is there generally another good practice to have just on one specific wordpress page custom php code? Something like a template for post ID?
If this was a page, you could have used a template file called page-{slug}.php, where {slug} is the slug of the specific page (see here: https://developer.wordpress.org/themes/template-files-section/page-template-files/#page-templates-within-the-template-hierarchy).
But since we're talking about CPT there is unfortunately only single-{$posttype}.php and no single-{$posttype}-{$slug}.php (see here: https://developer.wordpress.org/themes/template-files-section/custom-post-type-template-files/#custom-post-type-templates)
However, you can add support for such template files for CPT by adding this code to your theme's functions.php:
add_filter( 'single_template', function( $template ) {
global $post;
if ( $post->post_type === 'guitar' ) {
$locate_template = locate_template( "single-guitar-{$post->post_name}.php" );
if ( ! empty( $locate_template ) ) {
$template = $locate_template;
}
}
return $template;
} );
And then use the template file single-guitar-myspesificguitarslug.php (where myspesificguitarslug is the slug of the CPT page where you want this template to apply)

Using timber to render plugin pages

I am making a plugin which adds a bunch of pages to the admin view of WP. I'd really like to use Timber, specifically, the Twig templating functionality to render these pages.
While I have next to zero experience in WP and PHP in general, what attracts me to this approach is my previous familiarity with Django / Flask templates which allow me to extend a base template and specify blocks for header, content, footer. That seems trivial to do with Timber when using it to create a theme, but I can't for the life of me figure out how to make this setup work within a plugin. Sure, I can do something like this:
add_action( 'admin_menu', 'test_setup_menu' );
function test_setup_menu() {
add_menu_page(
'Tables',
'Tables',
'manage_options',
'test-tables',
'admin_page_test'
);
}
function admin_page_test() {
Timber::Render( 'test.twig');
}
But that of course will render test.twig with header and footer parts already populated from the theme. The issue specifically is that I want to be able to add information to the header or footer blocks. I know I can do this like so:
add_action('admin_head', 'add_to_head')
function add_to_head() {
...
}
But this is precisely the type of thing I'm trying to avoid, I wish to encapsulate this type of logic in a Twig template. Is there any way to make this work?
Here's an example of how to add a custom admin page for a plugin.
<?php
/**
* Plugin Name: Test Run
*/
add_action('admin_menu', 'admin_menu_cb');
function admin_menu_cb()
{
// Ref: https://developer.wordpress.org/reference/functions/add_menu_page/
add_menu_page('Test Run Admin Page', 'Test Run', 'manage_options', 'test-run', 'render_menu_page_cb', 'dashicons-schedule', 3);
}
function render_menu_page_cb()
{
Timber::$locations = __DIR__.'/views';
$data = [];
Timber::render('main.twig', $data);
}
For a more full example please see the below repo. I created it recently as a guide for anyone to use Timber in a wordpress plugin.
https://github.com/chanakasan/a-wordpress-plugin-using-timber

How to remove wordpress date archive pages?

I have developed my own wordpress theme, and learning all kinds of programming stuff, but my priority is the content, not the programming knownledge. I need to know how to remove archive date listing from wordpress?
Question 1: The google search results displayed like this: virmodrosti.com/2017/05/
I don't want any kind of date archive option, how do you disable that?
I also don't use any kind of plugin, and always like to do it on my own.
Question 2: I don't know why older entries doesn't work anymore
virmodrosti.com/zdravje/ this page works fine
virmodrosti.com/zdravje/page/2/ it redirects to 404 error page
I only choose option in wordpress to hide that annoying /category/ with dash . inside editor at permanlinks, Category base. Maybe somehow these stuff is kinda fighting with each other and doesn't work properly.
Thank you.
This is code from Digital Nomad theme I maintain:
function digitalnomad_remove_date_archives() {
//if we are on date archive page
if ( is_date() ) {
// theme sets alternatine archive page with table like list of all posts
$archive_page = get_option( 'digitalnomad_archive_page' );
if ( $archive_page ) {
// redirs to alternatine archive page if configured (good for SEO)
wp_redirect( esc_url( get_page_link( $archive_page ) ) );
die();
} else {
// otherwise error 404 is displayed
global $wp_query;
$wp_query->set_404();
}
}
}
add_action( 'template_redirect', 'digitalnomad_remove_date_archives' );
Use the smart Archive Page Remover wordpress plugin
or visit your theme's functions.php file
then insert this code
/* Register template redirect action callback */
add_action('template_redirect','makes_remove_wp_archives');
/* Remove archive*/
function makes_remove_wp_archives(){
// if we are on category or tag or date or author archive
if(is_category()|| is_tag()||is_author()){
global $wp_query;
$wp_query->set_404();
}
}

how to call complete my custom php page without header and footer of wordpress with shortcode or a custom page

I am creating a Plugin, as that plugin activated a "custom post type" also created named "documentation" also a page named "Documentation" created.
Now I want that whenever "Documentation" page is viewed it should show completely my custom php page. I dont want wordpress header, footer etc.
I want this page completely in my hand.I tried with shortcode, I created shortcode and put it in page content but when this page is viewed it shows shortcode content with worpress theme layout (header, footer).
Following is my code.
function documentation_fun( $atts ) {
ob_start();
include 'documentation.php';
return ob_get_clean();
}
add_shortcode( '3pane-documentation', 'documentation_fun' );
I am creating this in my plugin.Please suggest me the best way to achieve my goal.
EDIT
I found something else as well, there is a filter for the content
function magicalendar_get_event_page( $content ) {
global $post;
if ($post->post_title == ' Documentation') {
$single_template = dirname( __FILE__ ) . '/documentation.php';
}
return $single_template;
}
add_filter( 'the_content', 'magicalendar_get_event_page' );
but this is just returning file path not the content of file (html). Also header and footer still coming which I dont want.
I found hook by myself :) it is add_filter('taxonomy_template');
Thanks!
If you want to remove header and footer then create your custom page template. its best option.
Create your template file.
<?php
/**
* Template Name: My Custom Page Template.
*
* #package WordPress
* #subpackage Twenty_Fourteen
* #since Twenty Fourteen 1.0
*/
And in end select this template in page.
so it will not print header and footer.
It will only print header and footer when you call.
get_header();
get_sidebar();
get_footer();
if not then it will out put your content only with custom header and footer.

Custom 'Edit Profile' Location in Wordpress

What I am trying to do is have the Edit Profile option in the Admin bar at the top of a wordpress page go to another url.
Basically instead of it going to the /wp-admin directory
I am trying to get it to go the the /editprofile.php page.
You can use the filter edit_profile_url:
add_filter( 'edit_profile_url', 'custom_profile_link_so_19216787', 10, 3 );
function custom_profile_link_so_19216787( $url, $user, $scheme )
{
return site_url( 'editprofile' );
}
I suppose that /editprofile.php is a page using this template and that its address is example.com/editprofile. If that's not the case, put a full URL instead of site_url($slug).
Related: Where to put my code: plugin or functions.php?

Resources