How would I structure this url functionality in wordpress - wordpress

What I'm trying to do in Wordpress is import movie data from the movie database API, and display it inside a wordpress site. I'm just having trouble figuring out what would be the best way to set this up in the backend with a clean url structure.
I created a template file, and its going to have a GET variable with the movies title in it. I would want my domain to look like
mydomain.com/movie/TITLEGOESHERE
how could i set this up through wordpress, so the page I created works that way.
This would not be pulling post data from inside the wordpress. Would htaccess work here?
EDIT:
The way im thinking of it right now with a dirty url is, mydomain.com/pagewithAPIcode/?movie=MOVIETITLE, that way I can get the MOVIE TITLE as a GET variable, and then pass it to the api to grab the external movie info. And then with htaccess, rewrite it so it looks nicer

In the following code, a query variable mvtitle is added in which the movie title will be available. A rewrite rule is also created which for addresses matching the movie/{some-title} scheme displays the page movie (post type page). To read value of the mvtitle use get_query_var() function.
add_filter( 'query_vars', 'so59386348_query_vars' );
add_filter( 'page_rewrite_rules', 'so59386348_page_rewrites' );
function so59386348_query_vars( $vars )
{
$vars[] = "mvtitle";
return $vars;
}
function so59386348_page_rewrites( $rewrite_rules )
{
$page_slug = 'movie';
$rewrite_rules["$page_slug/([^/]+){1}/?$"] = 'index.php?pagename='.$page_slug.'&post_type=page&mvtitle=$matches[1]';
return $rewrite_rules;
}
$title = get_query_var( 'mvtitle', FALSE );
echo 'Movie title: ' . $title;

Please follow below instructions:
Log in to your WordPress website.
When you're logged in, you will be in your 'Dashboard'.
Click on 'Settings'.
On the left-hand side, you will see a menu. In that menu, click on 'Settings'.
Click on 'Permalinks'.
The 'Settings' menu will expand providing you additional options. Click on 'Permalinks'.
Select 'Post name'.
Click 'Save changes'.
I hope it would help you out.

Open dashboard
Search for settings
Click to permalinks in settings
Look for Permalinks common settings
in Custom structure use the url like %category%/%postname% and save
Enjoy!!

First of all open the admin panel, Go to settings->permalinks and in the custom settings, change the custom structure such as:
%custom_name%/%postname%
You can change the custom name with category or any other static name you want.

Related

Plugin Development: Dynamically change post title (the_title()) from a plugin, without changing database title

I am developing a custom plugin that needs to change the post title dynamically (NOT the "title" tag for the head) without using a page template.
I want the end user of the plugin to be able to use their own theme templates, which will already output the title, usually in an h1 tag, but I don't know where and how for sure, because it is not my theme.
I will need to just change the output on the frontend, not in the database. I want to change it based on whether a url's query string matches certain values, or whether the frontend user is logged in or not. I know how to do this from a custom template, but I want to do this from a template the user already has selected.
I noticed that if I filter the_title(), it changes it everywhere in the admin dashboard and in the frontend navigation menus, which is what I don't want, I just want to change where it is outputted in the user's page template (usually in an h1 tag).... does anyone have any suggestions on how to go about this?
You can use the_title filter, which would have influence only on front-end, this doesn't change record in Database. Your code would be something like this:
add_filter( 'the_title', 'add_text_to_post_title' );
function add_text_to_page_title( $title ) {
if( is_post() )
$title = 'Your text: '. $title;
return $title;
}

WordPress functions.php - Admin html injection and submitting forms

I created a new navigation item on the left for my WP Admin:
add_action( 'admin_menu', 'addManagementMenuItem' );
function addManagementMenuItem(){
add_menu_page('Issue Management', 'Issue Management', 'manage_options', 'issue_management_slug', 'issue_management_building_function','',3);
}
function issue_management_building_function(){
if(!current_user_can('manage_options')){
}
else {
?>
...
...
So where I have the ellipsis ... is where my HTML begins and I write out some information to the page with various php echo statements to print some data out.
What I would like to do is now give the user the ability to enter in a filter and press submit. This would issue a POST to another page which would receive the post data, run some stuff, and spit out something else to the screen. I was just thinking this would take the user away from the WP-ADMIN area entirely (what I want to do is keep the user all within the right pane so it looks like it's natively happening on WordPress under my new admin area)
Something feels wrong about this approach above where I'm putting tons of html into functions.php - what is the way to create pages for a custom admin section where I can do things like post forms and go to multiple pages?
I was thinking the best solution would be to put an iframe in my injected HTML in functions.php, and then the pages can talk to themselves just like normal behind the scenes in WP-admin.
Could anyone point me in the right direction?
thanks!
Considering the user input/_POST features you'd like to add to this, you may want to consider building this functionality out as your own plugin. I've always kept custom functionality limited to non-user interaction in the functions.php file, but anything further would probably be better fit as it's own plugin.
For example, what if you created a plugin directory named nullhypothesis:
add_action( 'admin_menu', 'addManagementMenuItem' );
function addManagementMenuItem(){
add_menu_page('Issue Management', 'Issue Management', 'manage_options', 'nullhypothesis/file_to_do_your_bidding.php', 'issue_management_building_function','',3);
}
It's that fourth parameter that in the documentation mentions that you should include the menu_slug, but it doesn't necessarily need to only be a function - it can also be a file you define.
Then, in your file_to_do_your_bidding.php file (within your plugin), you can add whatever _POST functionality you'd need it to. It could also exist as the 'admin' page that the administrator/whoever interacts with.
Was that what you were looking for?

Admin URL to my plugin's page

My plugin is basically a link display page, for instance if you want to display a page with links to other websites.
In wp-admin I have a menu item on the left side bar added with this code:
function bls_add_menu_page() {
add_menu_page('Custom Links', 'Custom Links', 'manage_options',
'customlinks', 'bsl_admin_page', '', 15);
}
After adding a new link, I want to redirect to my plugin home page in admin. The URL when I click on my plugin menu link is :
localhost/wp-admin/admin.php?page=customlinks
How do I get that URL in Worpdress? Currently I just do this :
wp_redirect('/wp-admin/admin.php?page=customlinks');
but I hope there is a better way of getting my plugin admin URL?
You get the concrete URL to admin.php by using the admin_url function:
admin_url('admin.php'); # http(s)://localhost/wp-admin/admin.php
That function chooses the proper sheme (http/https) based on your Wordpress configuration for you so you do not need to care about it. Same for the path to the admin. The only thing you need to specify is the file name (admin.php).
And in your concrete example you add the page query-info part:
$url = admin_url('admin.php?page=customlinks');
wp_redirect($url);
URL for menu page or options page has 'page' parameter ( page slug defined in add_menu_page() or add_options_page() ). You can always get the current page from $_GET['page'] param, so URL for the options page is:
admin_url( "options-general.php?page=".$_GET["page"] )
, and URL for menu page ( actually it works with options pages also ) is:
admin_url( "admin.php?page=".$_GET["page"] )

is it possible to make a custom page in buddypress

is it possible to make a custom page for buddypress with its url like this: http://domain.com/custom_page ? I found some answers by searching google but it does not create a custom page. i have a code here that i found in one of the blogs.
define('BP_FUN_SLUG','fun');
function bp_show_fun_page() {
global $bp, $current_blog;
if ( $bp->current_component == BP_FUN_SLUG && $bp->current_action == '' ) {
// The first variable here must match the name of your template file below
bp_core_load_template( 'fun', true );
}
}
add_action( 'wp', 'bp_show_fun_page', 2 );
but this code does not work... Is anyone there knows how to do this? thanks
Yes, it is possible to create a new page in Buddypress.
In Buddypress you have to create a new plugin or create a function in the theme functions file.
For creating first you have to add a new page link in navigation menu using bp_core_new_nav_item() function (You have created sub menu for that use bp_core_new_subnav_item() function).
Above two functions pass the screen function name as a parameter this name use when you click the custom page link call to this screen function. Create new function in your functions.php file same as screen function name. In this function call to your custom template file using bp_core_load_template() function.
Then finish, add more logic to create a new function and call it in the template file.
Another approach is to add a plugin that allows php in posts. For example http://wordpress.org/extend/plugins/allow-php-in-posts-and-pages/
Then create a page and add this to it:
[php] locate_template( array( 'some-template-folder/something.php' ), true ); [/php]
In case someone is wondering how to integrate custom pages into user profile (so that it looks like activity stream, groups etc.).
One thing i did recently was define a plugin (functions.php would work as well), register a custom slug with bp_core_new_nav_item or bp_core_nav_subnav_item and loaded member/single/plugins.php template in the handlers for those slugs. There are a bunch of actions on that page that you can hook into like bp_template_title and bp_template_content.
This can give you a whole lot of control.
Here's a link to the source of plugins.php: http://phpxref.ftwr.co.uk/buddypress/nav.html?readme.txt.source.html

WordPress add a new page to admin section

I have already developed my plugin for WordPress and I can manage it from admin. I have passed the access to the plugin file using add_submenu_page. The problem is that the plugin is extending and I want to use another file that is linked from the main file. For example I have second_page.php?id=3. When I try to access this link, I get a
You do not have sufficient permissions to access this page.
message. I want to "validate" this page also for using with this script and I don't know how. Ideas?
When you add a page with add_submenu_page(), the url should be something like:
wp-admin/admin.php?page=<your_page_handle>
Your page is actually loaded from admin.php (typically). You can add parameters to your links by appending something like &id=3 and then have your main plugin page-loading logic determine which file to include based on the parameter.
For instance
if (isset($_GET['id']) && ((int) $_GET['id']) == 3) {
include 'second_page.php';
} else {
include 'first_page.php';
}
Edit:
I found a trick that may be easier for you, though I haven't thoroughly tested it. Let's say that you have two pages: my_one and my_two. Just call add_submenu_page twice, and set the second page's parent as the first page. This will cause Wordpress to not add a link to the navigation bar, but you can still access your page by navigating to admin.php?page=my_two.
Example:
add_submenu_page(
'my_toplevel_link'
, 'Page Title'
, 'Link Name'
, 'administrator'
, 'my_one' // here's the page handle for page one
, 'my_one_callback'
);
add_submenu_page(
'my_one' // set the parent to your first page and it wont appear
, 'Page Title'
, 'Link Name' // unused
, 'administrator'
, 'my_two'
, 'my_two_callback'
);
Since WP natively supports URLs like wp-admin/admin.php?page=<your_page_handle> you can do sub pages with something like:
wp-admin/admin.php?page=yourpage
wp-admin/admin.php?page=yourpage&sub=2
wp-admin/admin.php?page=yourpage&sub=3
Then in the code that handles wp-admin/admin.php?page=<your_page_handle> you just look at the $_GET and pull up the main page or a sub-page as needed.
I've definitely seen plugins where the admin page has a little row of links across the top linking the various sub-pages.

Resources