Get full rendered Wordpress page with WPML via REST API - php

I've made a Laravel-Wordpress connection for a project. For this connection, I use the Worpdress REST API.
I'm able to get specific pages from Laravel with the header and footer from Wordpress via API (for a uniform look) and other pages comes completely from Wordpress. Big benefit is, that Wordpress pages can be edited with the pretty user-friendly Wordpress backend while other pages can be custom coded in Laravel.
To achieve this, I have added some new API endpoints to retrieve the rendered header / footer and a complete page:
add_action('rest_api_init', function () {
// add 'rendered_page' member with complete HTML for requested page
register_rest_field(
'page',
'content',
array(
'get_callback' => 'by_render_page_content',
'update_callback' => null,
'schema' => null,
)
);
// add new endpoint for getting header and footer
register_rest_route('eeg/v1', '/headerandfooter', array(
'methods' => 'GET,POST',
'callback' => 'get_page_header_and_footer',
));
});
The callbacks look like this: (i.e. for the complete page)
if (isset($request['language'])) {
do_action('wpml_switch_language', $request['language']);
}
$file = THEME_DIR . '/single.php';
ob_start();
include $file;
return ob_get_clean();
As you can see, I'm checking for a language parameter and set the current language of the WPML plugin with wpml_switch_language hook.
This seems to work 'a little bit'. So, for example, the language switcher shows the correct current language.
The problem is, in the main menu: All links are showing to default language. For example if currently selected language is english, the links should look like mydomain.com/en/requestedpage. But all links go to the default mydomain.com/requestedpage. Also the <html lang> parameter is set to the default language, not to the requested. And also the <link hreflang> tags from WPML are missing.
If I access the page via the Wordpress Frontend (which is hosted at a subdomain), everything is working correctly.
So I think, I have to set the requested language somewhere else or have to call some WPML 'prepare' hooks or something like that, to make this work. Maybe also the include './single.php'; is not the right way to do this.
Any hint is welcome.

I could fix it by myself.
The solution was to create a translated menu with WPML.
In Wordpress frontend, the links in the menus are localized also without an extra translated menu. But for this scenario with the REST API, a translated menu is necessary.

Related

Enqueue fontastic.me as 'dashicons' without blocking existing Wordpress dashicons

What I am trying to achieve:
I want to use custom icons on my Wordpress website (including in the Admin Menu).
What I have done so far
I have set up an account on fontastic.me and enqueued the font using the following in functions.php hooked to 'admin print styles'
wp_enqueue_style('fontastic-css', 'https://file.myfontastic.com/<mycode>/icons.css');
I've set the name of my icons on fontastic me to be 'dashicons' because this way you can directly use them in the admin menu e.g.
add_menu_page(
'Page Title',
'Menu Title',
'Capability',
'menu_slug',
[ $this, 'call_back' ],
'dashicons-custom-icon-name',
1
);
If you call them something else e.g. custom-icons you can't use them so easily.
I have also tried enqueuing in other ways but the issue is the same as I document below.
What's the issue?
However, by doing this I am evidently overwriting / blocking / generally interfering with the existing 'dashicons' library provided with Wordpress.
This means any dashicons throughout Wordpress that are not in my library fail to load and I am left with empty squares in their spaces.
Any help identifying how I can add to the dashicons library, rather than overwrite it would be much appreciated. Thanks.
A solution to this problem is to do the following:
1) Go back to fontastic.me and change the font so it is no longer 'dashicons' but something else e.g. 'customicons'.
2) Remove the code 'dashicons-custom-icon-name' from the menu function - let it default to the standard 'dashicons-admin-generic'.
3) Download your font from fontastic me and open it an an editor - so you have a reference of all of the icon codes - I can't find these on the website.
4) Add the following to your style sheet for every icon you would like to target
#adminmenu li.toplevel_page_bnfo_database div.wp-menu-image.dashicons-admin-generic:before { // change 'menu_slug' to whatever your menu slug is
font-family: yourfont; // this needs to be whatever you called it on fontastic.me
content: "\62"; // Whichever icon you want to show.
}
Why this isn't a perfect solution
This seems to require more code and be more cumbersome than the solution I was originally pursuing.
However, it is the solution that plugin providers (e.g. WooCommerce) are using so might be the best bet.

How to change Sign up page design in Moodle

I am new to moodle and i am using 2.9. I have a separate design for Signup page (HTML5 & CSS). How to integrate this page ?
I know the process of changing the default Login page like this
In config.php of my theme
'login' => array(
'file' => 'login.php',
'regions' => array(),
'options' => array('langmenu'=>true),
),
Where my Custom login page is login.php which is placed in my layout folder. So in this way i can takeover Login page design with my new design.
But i don't see any signup array in config page for Registration. So anyone can tell me how to do this change ?
Edit - I have checked this file moodle2\login\index_form.html I can see the signup design. But my issue is that file index_form.html has Moodle core CSS if i add my CSS there it will conflict also i dont know how to load the CSS from my theme folder to index_form.html.
Can anyone guide me ?
I already checked Moodle.org forum but not able to find the process.
Thanks In Advance
Lorry
Further to what #davosmith mentioned, you can actually use the existing auth/email plugin as a base for your plugin, So:
Copy auth/email and rename it to whatever you want, for example auth/foo. And rename all other instances of "email" to "foo".
Copy the login/signup_form.php to your plugin directory
in your auth/foo/auth.php file, add a function:
function signup_form() {
global $CFG;
require_once($CFG->dirroot.'/auth/foo/signup_form.php');
return new login_signup_form(null, null, 'post', '',
array('autocomplete'=>'on'));
}
Modify the auth/foo/signup_form.php to whatever you want
Hiii,
I'm one of the Moodler who working with Moodle.
to edit the signup page go at the following location and open the signup.php file.
./moodle/login/signup.php
in that file you can see at bottom of the page three lines.
...
echo $OUTPUT->header();
$mform_signup->display();
echo $OUTPUT->footer();
in which $mform signup->display(); create a form yu can comment and write HTML for your sign up form.
But make sure about the Action URL and the validations.
$mform is the object that create form and its class file is signup_form.php.
Hope it helpful for you ... Good Luck ['}
If you are wanting to avoid making changes to the core code in Moodle (which should almost always be what you want), then you can create a new signup form from scratch by first creating a new authentication plugin (in auth/NAMEOFPLUGIN).
When you create the authentication plugin, you should make sure that the 'can_signup' function returns true, that the 'user_signup' function does whatever processing is needed to create the new user account and that the 'signup_form' function returns a custom Moodle form that contains the fields you want.
It is possible to further customise this form by outputting custom HTML elements (using $mform->addElement('html', 'The HTML to output'); ). I would not advise completely abandoning the Moodle form (i.e. to replace it with hand-coded custom elements), as that will not be compatible with the signup code in login/signup.php (as well as losing the validation rules that are supported by the forms library).

When I use CListView widget in YII the fontsize of my whole website changes. (I use flowtype for fontscaling)

When I use my CListView Widget in a page the fontsize of my whole website changes. I use flowtype to scale my fonts and keep everything responsive. Only when I use this widget everything changes, nog only the data of the widget itself.
I tried to turn the css styling of the widget of and tried to control it via an own css file, but this doesn't matter.
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'cssFile'=>false,
)); ?>
The content of the view I'm calling doesn't matter either. Only when I remove the widget and put some text instead it changes back to normal.
Does someone have any idea what's happening here?
Quoted from official documentation for ClistView
cssFile property
public string $cssFile;
the URL of the CSS file used by this list view. Defaults to null,
meaning using the integrated CSS file. If this is set false, you are
responsible to explicitly include the necessary CSS file in your page.
Follow it, when you used CListView, default css .../listview/style.css file would be imported into your page, it could change your current layout if some of css is coincided with each other.
You can override it
$this->widget('zii.widgets.CListView', array(
'cssFile' => 'your-css-file-path.css',
...
)
So, eventually tried to copy the parsed code from the page source into my view. So in pagecode this was exactly the same output as the widget does in the page. Now I didn't had the resizing problem. So the widgets do something more then I can see.
For the solution I wrote an own widget to get my data in which I can control everything that happens. So no problem left anymore. Thanks for your help Telvin!

Drupal node 2-page content split

My employer requires certain pages on the website have a two page feature.
What this means is that some default content show up on the node_view page as normal but the second part should show up when a link is clicked.
This will be easy if I could do this across multiple nodes but the requirement is for all the data to be stored in one node but displayed across two pages.
I hacked together the following code:
function mymodule_node_view($node, $view_mode, $langcode){
$path = current_path();
$path_alias = drupal_lookup_path('alias',$path);
$links = array( 'test' => array('title'=>'my_link', 'query'=>'', 'href'=>"{$path_alias}/nextpage") );
$node->content['my_module'] = array(
'#theme' => 'links__node__mymodule',
'#links' => $links,
'#attributes' => array('class' => array('links', 'inline')),
);
}
That creates a hyperlink called my_link across the top of my content area - which is great.
The problem starts when I click the hyperlink. Supposing I am on http://example.org/homepage and I click the hyperlink, I expect to be redirected to http://example.org/homepage/nextpage. Also, I still want to maintain the view and edit tabs of the actual node I was on. However, Drupal correctly gives me a "page not found" error.
What's interesting is if I used http://example.org/node/1 and visited http://example.org/node/1/nextpage, I don't get the issues I described above but the url is less descriptive.
To solve this problem, I am sure I have to extend hook_menu but I don't know how to account for any number of taxonomy terms leading up to the actual node title. So, I can't predict how many % I will need before the node title and then my /nextpage. However, I want /nextpage to still have the view and edit tabs of it's parent page.
This is all unexplored territory for me.
Update
I found the following function which does a great job of returning the entire node path complete with taxonomies:
$path = current_path();
$path_alias = drupal_lookup_path('alias',$path);
What I don't know is how to take advantage of this in hook_menu to dynamically create /nextpage for my nodes.
Please remember, I don't really want /nextpage to be entirely independent of the original and actual Drupal node. When on /nextpage I want to be able to have access to the view, edit etc tabs of the node.
So, /nextpage effectively is just an extension of a Drupal node page.
There is a quick way to do that. Using views module.
In the fields section choose the field you wanna view. And in the arguments add the nid.
Then add the link to the node view you already created.
The final result http://mysite/views-page/[nid]
Hope this helps... Muhammad.
I would check the node_menu() function to get some reference on how it's implemented.
Not sure on your taxonomy requirements so this might be insufficient but I'll go with what I understand.
But off the top of my head I'd go for something like:
function mymodule_menu() {
$items['node/%node/nextpage'] = array(
'title' => 'Next page',
'type' => MENU_LOCAL_TASK, // Makes it a tab on node/%node-pages
'page callback' => 'mymodule_node_page_view', // Your page display function
'page arguments' => array(1), // First will be a node object, second will be whatever value is passed in the url
// You should rip access callback and access arguments from node_menu()
);
return $items;
}
That should do something like what you are asking for.
It is also possible, easier and definitely recommended to do this with Panels/Pages (see also Chaos Tools) or arguably Views as they are quite capable of all this and generally a better way to work with Drupal's strengths than custom code.
Updated
To clarify I've simplified the menu hook and you should be able to use the below page view function. I still believe you would make a better solution using Panels and overriding node_view and such.
The MENU_LOCAL_TASK part in the menu hook should turn this into another tab along with View and Edit.
function mymodule_node_page_view($node) {
die("It works: ".$node->title);
}
Hope that's more helpful.

Croogo translate route does not match when invoking helper->url from a plugin

I'm using Croogo 1.3.4 with Translate plugin. The url of my nodes look like:
fra/page/my-slug-for-page
When I access the page fra/page/my-slug-for-page , the Html->image method (invoking app_helper->url) is rendering following link: http://www.mysite.com/fra/page/my-slug-for-page
This is perfectly working.
However, when I'm rendering the same themed layout, in a plugin named myplugin, the Html->image method is rendering http://www.mysite.com/myplugin/page/my-slug-for-page/locale:fra
I traced into the Router:url method, and it appears that no route match, especially not this one that match in other cases.
Router::connect('/:locale/:controller/:action/*', array(), array('locale' => '[a-z]{3}'));
Image link is displayed like this:
<?php echo $html->image('/img.png',array('url' => array('locale'=>'fra', 'controller'=> 'page', 'action'=>'my-page-slug')));?>
I found that the problem was in plugin name that should be specified, even if empty. Croogo it self feed the plugin name with the current plugin name.
<?php echo $html->image('/img.png',array('url' => array('locale'=>'fra', 'controller'=> 'page', 'action'=>'my-page-slug', 'plugin'=>'')));?>

Categories