I try to call from my Codeigniter controller as outside of the box my Wordpress blog menu, but I get the following error
Fatal error: Cannot redeclare site_url() (previously declared in /../system/helpers/url_helper.php:53)
public function getWordpressMenu() {
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
// no theme output
define('WP_USE_THEMES', false);
// initializes the entire Wordpress
require $_SERVER['DOCUMENT_ROOT'] . 'blog/wp-blog-header.php';
wp_nav_menu('your_theme_menu_location');
}
I know both of frameworks have the same function but is there a way to disable on of inside of controller?
In Codeigniter you can simply replace your helpers by creating a new file at application/helpers
In your case, you can simply create a new file at application/helpers with name url_helper.php
Just copy the url_helper.php from the folder system/helpers and copy it to application/helpers/ . Now Open the file url_helper.php and rename the function url_helper to something else, for example: ci_site_url
Related
I am building a custom Gutenberg block an register the editor script as recommended in the block.json file.
"editorScript": "file:./index.js",
Now I would like to pass data from php to Javascript using the wp_add_inline_script() function. However this requires the handle of the script. In the block.json I don't name a handle and there doesn't seem to be a possibility to do it.
Is there something like a default handle? Or is there another way to pass data from my php plugin file to JavaScript (I want to pass the plugin directory path).
Any help is highly appreciated!
Yes, wp_add_inline_script() is used for passing data from PHP to JavaScript, including Gutenberg blocks.
The script handle you are looking for is the name of your block with the / replaced with a -, eg:
block.json
{
"name": "my/blockname",
"editorScript": "file:./index.js",
...
}
my-block.php
function my_blockname_add_inline_script()
{
// Plugin path for my block
$path = plugin_dir_url(__FILE__);
$handle = 'my-blockname'; // name from block.json with a '-' instead of '/'
$data = 'const myBlockPath ="' . $path . '";';
$position = 'before';
wp_add_inline_script($handle, $data, $position);
}
/**
* Registers the block using the metadata loaded from the `block.json` file.
*/
function my_blockname_block_init() {
register_block_type( __DIR__ . '/build' );
// Enqueue script after register_block_type() so script handle is valid
add_action('admin_enqueue_scripts', 'my_blockname_add_inline_script');
add_action('wp_enqueue_scripts', 'my_blockname_add_inline_script');
}
add_action( 'init', 'my_blockname_block_init' );
import a class from theme directory outside of wordpress.
The CRON
I am cronning a daily job via Cpanel for my site that will feed a stats table. I created a file in the root Home/myDirectory (same level as public_html ) beloo is the code
<?php
include dirname(__FILE__) . '/public_html/wp-load.php'; // Load WP Functions
include dirname(__FILE__) . '/public_html/wp-content/themes/cooking/config/Data/ViewsData.php'; // require a class
function test(){
if(class_exists('ViewsData')){
$viewsData = new ViewsData;
$views= $viewsData::getViews(394);
update_post_meta(394 , 'views_test', $views);
}
die();
}
test();
THE CLASS
as shown in the code I am trying to include a class from the theme folder. this class include different functionaries to set , get and update the views data> bellow is an idea of how the class is structured ;
namespace GS\Data;
use GS\DisplayFunc;
class ViewsData {
static function getViews(){}
}
however class_exists('ViewsData') always return false.
any suggestions on what is wrong. or even on how to reorganize the whole cron solution. the most important is that I need to use many classes from my theme folder.
I was able to find the problem. it has to do with namespaces the code that works bellow :
<?php
include dirname(__FILE__) . '/public_html/wp-load.php'; // Load WP Functions
include dirname(__FILE__) . '/public_html/wp-content/themes/cooking/config/Data/ViewsData.php'; // require a class
function test(){
if(class_exists('GS\Data\ViewsData')){
$viewsData = new ViewsData;
$views= $viewsData::getViews(394);
update_post_meta(394 , 'views_test', $views);
}
die();
}
test();
I would like my custom Drupal 8 module to force a different template for pages that match a certain URL. Here is my module structure:
/sass_edit
/css
/js
/template
builder_template.html.twig // this is the template file
sass_edit.module
...
sass_edit.*.yml // various yml files
These are the contents of builder_template.html.twig:
<h1>test</h1>
Here are the relevant lines of code in my .module file:
function sass_edit_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
$current_path = \Drupal::service('path.current')->getPath();
if(\Drupal::currentUser()->isAuthenticated() && strpos($current_path, "/builder/") >= 0) {
$suggestions[] = 'builder_template';
}
// kint($suggestions);
// die();
}
When I visit a page whose URL contains /builder/, the code runs and adds the string builder_template to the $suggestions array. However, when the page is rendered, the new template is ignored. I have tried flushing caches, with no results.
I'm not sure how to proceed. All the documentation I've found refers to adding theme suggestions from a custom theme, not a custom module.
Any idea what I'm doing wrong? Thank you so much for any help!
Maybe you have a typo in the code you pasted but the folder where you have your templates overrides should be templates and not template as you wrote. Don't forget to flush caches after changing the name of the directory ;)
I have removed the base_tag from my template to allow for the use of SVG icon sprites. A side effect of this is that images on pages other than the home page break.
The HTMLEditorField::saveInto() method is forcing a relative url on the image and they end up pointing to /some-page-other-than-home/assets/image.jpg which is wrong.
How do I get images to resolve to the root /assets/ directory rather than pointing to /some-page-other-than-home/assets/.
SilverStripe provides extension hooks on some methods. The HTMLEditorField has a 'processImage' hook that you can tap into.
You can hook into this method with the following:
1. Create your extension configuration.
/mysite/_config/Config.yml
HtmlEditorField:
extensions:
- HTMLEditorFieldExtension
2. Create a the HTMLEditorFieldExtension class.
/mysite/code/HTMLEditorFieldExtension.php
<?php
class HTMLEditorFieldExtension extends DataExtension
{
// This method name must be the same as the extension hook
public function processImage($image, $img) {
// Get the image src attribute
$src = $img->getAttribute('src');
// Concatenate a leading slash
$img->setAttribute('src', '/' . $src);
}
}
3. Run a dev/build.
This needs to be done so SilverStripe can find the new extension. After this your images should now have the missing leading slash.
I added new function which is called "css_url". I set in constructor of controller, It works.
Besides, I want to reach in assets in view. In view directory I put themes, and each themes has own assets. How can I reach assets in view directory?
-view
--theme1
---assets
----css
----js
----images
-----f1.jpg
enter code here
public function __construct() {
parent::__construct();
// Temel veriler veri tabanından çekilecek;
$this -> theme = "theme1";
$this->config->set_item('css_url', $this->theme);
}
css_url("images/f1.jpg");
goto application/config/autoload.php and url helper like this...
$autoload['helper'] = array('url');
Now you are able to access assets folder in your view using base_url() function like this...
<?php echo base_url('assets/css/yourstyle.css'); ?>
Place your assets folder on root of your application...
application/
assets/
system/