Wordpress dynamic url for same page template - php

I have created page template in wordpress. right now its url is static like http://localhost/wordpress/test-test1 but i want to make it dynamic like http://localhost/wordpress/test1-test-test2 . Is it possible to create a single page template having dynamic url??
I want both url's to comes on single page. If page templating approach is not good then what could be other approaches. Here the test urls:
http://localhost/wordpress/test-test1
http://localhost/wordpress/test1-test-test2
http://localhost/wordpress/test13-test2-test1
I found similar links but none of the link helped me.

Here it depends on how would you change to pass argument, Suppose the last stage and it should be same always, and let page name 'anything'
<?php
add_action('init', 'add_my_rule');
function add_my_rule()
{
global $wp;
$wp->add_query_var('args');
add_rewrite_rule('test\/laststage\/(.*)','index.php?pagename=Pagename&args=$matches[1]','top');
Apply your custom template to this page, & on that template use this:
//if you visit http://.../test/laststage/name/AnyName, thus this $params will be name/AnyName.Now need to explode this and get the value.
$params = get_query_var('args');
Here's info: http://www.workingwith.me.uk/articles/scripting/mod_rewrite

Related

WordPress replace page with a plugin

I am trying to make a plugin that will display custom page when a parameter is present in the URL. For example, let's say that the URL of a post is localhost/wp/2018/03/22/postname/, it displays post normally as expected. What I want to achieve, is when I specify URL as localhost/wp/2018/03/22/postname/?param=1, I want to display page that I will generate with the plugin. I made a function that works when the parameter is present and it echoes some example content, however, it merges with what WordPress normally displays. So when I put the parameter into URL I get a regular page with post and my content somewhere in the middle. I want to display only the page that i generate from scratch, from <html> to </html> with my plugin. How can I do that?
There is a filter in WordPress called template_include this filter is executed before any output is displayed on the WordPress generated page.
You can override the current template via this filter as soon as you find the parameter and value in the URL. E.g. below is a filter I was using in a project to override archive & single template for a certain CPT:
function em_templates($template) {
if(get_query_var('post_type') == 'xyz' ) {
return FMM__DIR__ . '/templates/archive-xyz.php';
}
return $template;
}
add_filter('template_include', 'em_templates', 1, 1);
You can adopt this logic for parameter and use this WordPress filter to take over the template process and display yours own.

Read POST data in Wordpress page

I am trying to write a plugin for wordpress. I have a problem i can't solve.
Inside plugin i habe added a shortcode that show a form.
function showForm() {
echo '<form method="post" action="www.example.com/myDestinationPage">';
[...]
}
add_shortcode( 'ShowFormSC' , 'showForm' );
After that, in a page, i added the shortcode and it works perfectly. ;-)
Now the problem: how can i read POST data in myDestinationPage (another wordpress page)?
In Php would be very simple ... but in wordpress I do not know how to do.
Second problem: myDestinationPage must be a real wordpress page with another shortcode inside, or can be defined as a "virtual" page inside my plugin?
Thank you for your help!
Best Regards,
Simone
www.example.com/myDestinationPage needs to be edited to recieve the post data, just as in any other php script, wordpress or not. If 'myDestinationPage' resolves to dynamic wordpress content, then you are in muddy waters.
Let's say, myDestinationPage is a wordpress Post. That "page" doesn't exist as a file, it comes directly from the database.
You could write a shortcode which handles this though:
add_shortcode('post_parser', 'postParser');
. . .
function postParser() {
filter_input(INPUT_POST, 'my_post_value');
//do something
}
Then, you just add the '[post_parser]' shortcode to the myDestinatioPage Post. (You mentioned it's a wordpress page, but page & post are both WP_Post objects.)
Another option is to put your post processing code in the post.php (or whichever template myDestinationPage is).
1st answer: you can directly use $_POST in wordpress like in php.
2nd answer: Yes you can. If you want to use pages in your plugin, use plugins_url() to generate the path for form action.
https://codex.wordpress.org/Function_Reference/plugins_url

Loading wordpress plugin on specific page only

I am developing a plugin for wordpress that loads javascript in a page.
But i want this plugin to load only on selected pages. Not all pages.
Can someone suggest how to do that?
Here is the code.
add_action('wp_enqueue_scripts', 'soundninja_enqueue');
function soundninja_enqueue($hook)
{
wp_enqueue_script('soundninja', // id
'http://soundninja.github.io/SNtest/build/Soundninja.min.js', // path
array('jquery'), // dependencies
0, // appends ?ver=$wordpress_version
true // in_footer
);
}
Another possible workaround would be to keep the plugin deactivated and activate it only for the required pages. This would prevent the additional overhead involved in loading the plugin on pages where the plugin is not required. And most importantly you do not have to tweak the code of the existing plugin.
Here is the post which can give you more idea http://shibashake.com/wordpress-theme/how-to-selectively-load-plugins-for-specific-pages
Depending on where you will call your function soundninja_enqueue($hook) you can easily add an if statement asking if the current page/post id is in an allowed list of ids.
But first you need to get the current page/post id, for this you have a couple of options, depending if are calling the function inside or outside of The loop.
see here on how to get the current page id in wordpress
<?php
$allowedIds = array(12,13,14);
if (in_array($currentID,$allowedIds)) soundninja_enqueue($hook);
Another option is to pass the the current page/post id as a parameter to the function and do the same if test inside the function, your choice.

Accessing $_SESSION from outside of Joomla

I used Joomla 1.5 for a site that I developed for a gaming company. Part of the site consists of a character generater for the game that they developed. The issue is that users want to be able to print the character sheets off, without having the Joomla template surrounding it.
As for the specifics, I have the directphp extension installed, and the entire generator is written in PHP (with a little JavaScript to handle things that PHP can't). As the generator spans several dozen page calls, it made sense to store everything in $_SESSION. All of this works correctly. In an attempt to make the final sheet printer friendly, I tried redirecting the user to a page outside of Joomla (though on the same server, and even within the same folder) but I cannot access the $_SESSION data from this new page.
I have seen several posts (a few on this site) that point to loading the Joomla Framework and accessing it that way, which I have tried, but the data that I was looking for does not appear to be contained there. Has anyone come across this problem before, or know how to get to that data?
You are making this WAY harder than it needs to be. You don't have to write any additional code to accomplish what you are trying to do. In order to print our the component output without all of the Joomla template, you just append ?tmpl=component to your URLs and Joomla will display only to component output without any of the template. If you want to give it a custom stylesheet or anything special, you can also add in a template override by adding a file named component.php in your template folder.
In order to control the CSS per page, you can add Page Class Suffixes in the menu items. Then add this code to index.php so you can use them.
Somewhere in the head add this:
$menu = &JSite::getMenu();
$active = $menu->getActive();
$pageclass = "";
if (is_object( $active )) :
$params = new JParameter( $active->params );
$pageclass = trim($params->get( 'pageclass_sfx' ));
endif;
Replace your body tag with this:
<body id="<?php echo $pageclass ? $pageclass : 'default'; ?>">
Any page that you do not specific a Page Class Suffix for will use default as the body ID, any that you do will use what ever you specify.

Symfony switch language

I use symfony 1.4.11 .User can switch language , in my layout I have
<?php foreach (sfConfig::get('app_cultures_enabled') as $key => $lng): ?>
<?php echo link_to_if($sf_user->getCulture() != $lng,
"<img src=\"/images/flags/$lng.png\" alt=\"$lng\">",
preg_replace('/sf_culture=[a-z][a-z]/', 'sf_culture='.$lng, $sf_context->getRouting()->getCurrentInternalUri(true)))?>
<?php endforeach ?>
So in each page I generate link, that I can switch language.
But I have page with next rout:
page:
url: /:sf_culture/page/:slug
param: { module: page, action: show}
requirements: { sf_culture: (?:se|no|en), sf_method: GET }
And when I go to this page I have error, that :
The "/:sf_culture/page/:slug" route has some missing mandatory parameters (:slug).
I think that problem in
<?php echo link_to_if($sf_user->getCulture() != $lng,
"<img src=\"/images/flags/$lng.png\" alt=\"$lng\">",
preg_replace('/sf_culture=[a-z][a-z]/', 'sf_culture='.$lng, $sf_context->getRouting()->getCurrentInternalUri(true)))?>
it can not generate link with slug, or..
If I'm reading this right, it looks like you're trying to mimick the slug variable using $sf_context->getRouting()->getCurrentInternalUri(true).
I've never done a language switch link this way but basically if that works, you need to pass it as a variable called "slug" to the routing. Somewhere in that magical link_to_if() function you need to have:
'&slug=' . $slug // the slug!
... given that slug is a named parameter that the routing rule is expecting.
UPDATE:
This is how I'd normally change language:
I'd create a separate action (e.g. changeLanguage) without a template, and then create links to it with the language as a parameter:
mysite.com/change-language/fr/ (?lang=fr)
mysite.com/change-language/de/ (?lang=de)
mysite.com/change-language/es/ (?lang=es)
In that action, I use $this->getUser()->setCulture($lang) to change language and anything else I want to do, and then use $this->redirect($request->getReferer()) to redirect the user back to the page they came from. If your app needs to use multi-language URLs, I would ensure that those get rendered on their own pages from a URL parameter (like the "slug"). And if you're doing this with SEO in mind, things also get a little more complicated because you should link directly to different language pages instead of using re-directs etc (to pass link juice).
Hope that helps.

Categories